xxxxxxxxxx
import pandas as pd
# Create a list of dictionaries with new data
list_of_dictionary = [
{"date": "2019-11-03", "small_sold": 10376832, "large_sold": 7835071},
{"date": "2019-11-10", "small_sold": 10717154, "large_sold": 8561348},
]
# Create a dictionary of lists with new data
dict_of_list = {
"date": ["2019-11-17", "2019-12-01"],
"small_sold": [10859987, 9291631],
"large_sold": [7674135, 6238096]
}
# Convert list into DataFrame
avocados_df1 = pd.DataFrame(list_of_dictionary)
avocados_df2 = pd.DataFrame(dict_of_list)
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
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)
xxxxxxxxxx
In [11]: pd.DataFrame(d.items()) # or list(d.items()) in python 3
Out[11]:
0 1
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391
3 2012-06-28 391
In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
Out[12]:
Date DateValue
0 2012-07-02 392
1 2012-07-06 392
2 2012-06-29 391
xxxxxxxxxx
df = pd.DataFrame({'col1': [1, 2],
'col2': [0.5, 0.75]},
index=['row1', 'row2'])
>>> df
col1 col2
row1 1 0.50
row2 2 0.75
>>> df.to_dict()
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}
xxxxxxxxxx
input->
a b
0 red 0.500
1 yellow 0.250
2 blue 0.125
dict(df.values)
output -> {'red': '0.500', 'yellow': '0.250', 'blue': '0.125'}