Pandas DataFrame
Overview
Matrix
import numpy as np
test = pd.DataFrame([[1,1],[1,0]])
test2 = pd.DataFrame(np.array([[1,1],[1,0]]))
val = int(test[0][0])
Constructing with Columns
Passing a dictionary into the DataFrame will take the dictionary keys to be the column names.
import pandas as pd
df = pd. DataFrame({
'column1':[1,2,3],
'column2':[4,5,6]
})
test = df['column1'][1]
test_int = int(test)
cols = df.columns
for col in cols:
pass
Row and Column Labels
test = pd.DataFrame([[1,1],[1,0]])
columns = ['column1', 'column2']
rows = ['row1', 'row2']
dataset = pd.DataFrame(data=[[1,1],[1,0]], columns=columns, index=rows)
print(dataset)
value = dataset['column2']['row1']
print(value)
Accessing Rows
You can access a row in a DataFrame by using either
- loc
- access a row by the row label
- iloc
- access the row by a numeric index
columns = ['column1', 'column2']
rows = ['row1', 'row2']
dataset = pd.DataFrame(data=[[1,1],[1,0]], columns=columns, index=rows)
row2 = dataset.loc['row2']
row2_again = dataset.iloc[1]
value = row2['column1']
value = dataset['column2']['row1']
print(value)
If a single row is returned, it is returned as a
Series object.
If multiple rows need to be returned (because they share the same row label and are returned from loc)
then a DataFrame object is returned.
Converting to/from Basic Types
A DataFrame can be created from a simple list of dictionaries as follows:
import pandas as pd
# 1. Define your list of dictionaries
data = [
{"Name": "Alice", "Age": 25, "City": "New York"},
{"Name": "Bob", "Age": 30, "City": "Chicago"},
{"Name": "Charlie", "Age": 35, "City": "San Francisco"}
]
# 2. Convert to DataFrame
df = pd.DataFrame(data)
print(df)
Likewise, a DataFrame can be converted to a list of dictionaries.
# 2. Convert to a list of dictionaries
list_of_dicts = df.to_dict(orient='records')
Extracting a Numpy Matrix
import pandas as pd
import numpy as np
# 1. Create a sample DataFrame
data = {
'A': [10, 20, 30, 40],
'B': [11, 21, 31, 41],
'C': [12, 22, 32, 42],
'D': [13, 23, 33, 43]
}
df = pd.DataFrame(data, index=['row1', 'row2', 'row3', 'row4'])
# Define your target list of row and column labels
target_rows = ['row2', 'row4']
target_cols = ['B', 'D']
# 2. Extract the Inner DataFrame
matrix = df.loc[target_rows, target_cols]
print(matrix)
Extracting a Numpy Matrix
# 2. Extract the NumPy matrix
matrix = df.loc[target_rows, target_cols].to_numpy()
print(matrix)