Convert pandas DataFrame to Dictionary in Python (Example) | Create dict Object | to_dict() Function

preview_player
Показать описание
Python code of this video:

import pandas as pd # Import pandas

data = pd.DataFrame({'x1': [4], # Create pandas DataFrame
'x2': [3],
'x3': [2],
'x4': [1]})
print(data) # Print pandas DataFrame

print(my_dict) # Print dictionary
# {'x1': {0: 4}, 'x2': {0: 3}, 'x3': {0: 2}, 'x4': {0: 1}}

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Thank you for this clear explanation! I have a question: what if I have a big dataset with multiple columns and I want to assign one column as a key and another as a value to create a dictionary out of it?