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 attributesimport pandas as pd
data = {"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}
df = pd.DataFrame(data)
df.at[0, "x"] = 10
df.iat[0, 1] = 11
df.loc[0, "z"] = 12
df.iloc[1, 0] = 13
df["y"][1] = 14
df/tmp/ipykernel_504978/4257647607.py:9: ChainedAssignmentError: A value is being set on a copy of a DataFrame or Series through chained assignment.
Such chained assignment never works to update the original DataFrame or Series, because the intermediate object on which we are setting values always behaves as a copy (due to Copy-on-Write).
Try using '.loc[row_indexer, col_indexer] = value' instead, to perform the assignment in a single step.
See the documentation for a more detailed explanation: https://pandas.pydata.org/pandas-docs/stable/user_guide/copy_on_write.html#chained-assignment
df["y"][1] = 14
| x | y | z | |
|---|---|---|---|
| 0 | 10 | 11 | 12 |
| 1 | 13 | 5 | 8 |
| 2 | 3 | 6 | 9 |
df[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()