Array

An array is a data structure consisting of a collection of elements, each identified by at least one array index or key.

Array Slicing

Array slicing is a function to create a new array from the elements of an existing array. Examples are:

# Create an original array.
original_array = [0,1,2,3,4,5,6]

# Create a new array specifying the start (inclusive) and end (exclusive) elements.
new_array_1 = original_array[0:2]
print(new_array_1)
[0, 1]

# Create a new array specifying the start (inclusive) and all following elements.
new_array_2 = original_array[3:]
print(new_array_2)
[3, 4, 5, 6]

# Create a new array specifying the start (inclusive) and end (exclusive) elements.
new_array_3 = original_array[2:5]
print(new_array_3)
[2, 3, 4]

Python Example

Below is a Python example using NumPy and Pandas functions:

# Python uses the lists construct to create arrays; indexing begins with 0.
array1 = ["element a", "element b"]
array1_first_element = array1[0]
array2 = [10, 67, 43]
array2_third_element = [2]

# The Numpy library can also be used to create an array.
import numpy as np

# Create an array using a list with type float.
array3 = np.array([[1, 2, 8], [4, 9, 7]], dtype = 'float') 
print (array3)

# Create an array of sequenced integers in steps of 5.
array4 = np.arange(0, 30, 5)
print(array4)

# The Pandas library can also be used to create and work with arrays.
import pandas as pd

# Create a Pandas array, known as a DataFrame.
dataframe1 = pd.DataFrame({'A': [5, 7, 20, 13], 'B': [67, 12, 8, 57]})
print(dataframe1)

# Split a DataFrame by row/column value.
dataframe2 = dataframe1[dataframe1.A > 10]
print(dataframe2)