xxxxxxxxxx
df.set_index('columnName').T.to_dict()
xxxxxxxxxx
#Lazy way to convert json dict to df
pd.DataFrame.from_dict(data, orient='index').T
xxxxxxxxxx
import pandas as pd
my_dict = {key:value,key:value,key:value, }
df = pd.DataFrame(list(my_dict.items()),columns = ['column1','column2'])
xxxxxxxxxx
>>> df.to_dict('records')
[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]
xxxxxxxxxx
create a dataframe from dict (function)
import pandas as pd
def create_df():
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
return pd.DataFrame.from_dict(data)
create_df()
xxxxxxxxxx
import pandas as pd
# Assuming you have a DataFrame called 'df'
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Convert DataFrame to dictionary
dictionary = df.to_dict()
print(dictionary)