import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5]) # 1D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 2d array
for array in arr_2d:
print(array)[1 2 3]
[4 5 6]
[7 8 9]
Python
The basic NumPy array looks like this:
data, index, columns, and dtype.df['column_name'] or df.column_name will return the column as a Pandas Series object.import pandas as pd
data = {"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}
df = pd.DataFrame(data)
print("x: ", df['x'][0])x: 1
locdf.loc[row, column].loc or iloc attributes over the index notation because it is more explicit and less prone to errors.at and iat attributesdf[df['column_name'] > value]We can also combine our operators.
& for and| for orsort_index()sort_index()sort_values()import pandas as pd
# Create a Pandas Series
s = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'], name='numbers')
sa 1
b 2
c 3
d 4
e 5
Name: numbers, dtype: int64
We can do much of the above on a Pandas Series.
iterrows()itertuples()items()