xxxxxxxxxx
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data)
xxxxxxxxxx
>>> data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data, orient='index')
0 1 2 3
row_1 3 2 1 0
row_2 a b c d
xxxxxxxxxx
>>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
>>> pd.DataFrame.from_dict(data)
col_1 col_2
0 3 a
1 2 b
2 1 c
3 0 d
xxxxxxxxxx
>>> pd.DataFrame.from_dict(data, orient='index',
columns=['A', 'B', 'C', 'D'])
A B C D
row_1 3 2 1 0
row_2 a b c d
xxxxxxxxxx
#Lazy way to convert json dict to df
pd.DataFrame.from_dict(data, orient='index').T
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
# 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)