1data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
2pd.DataFrame.from_dict(data)
1>>> data = {'row_1': [3, 2, 1, 0], 'row_2': ['a', 'b', 'c', 'd']}
2>>> pd.DataFrame.from_dict(data, orient='index')
3 0 1 2 3
4row_1 3 2 1 0
5row_2 a b c d
6
1>>> data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
2>>> pd.DataFrame.from_dict(data)
3 col_1 col_2
40 3 a
51 2 b
62 1 c
73 0 d
8
1#Lazy way to convert json dict to df
2
3pd.DataFrame.from_dict(data, orient='index').T
1# Basic syntax:
2import pandas as pd
3pandas_dataframe = pd.DataFrame(dictionary)
4# Note, with this command, the keys become the column names
5
6# Create dictionary:
7import pandas as pd
8student_data = {'name' : ['Jack', 'Riti', 'Aadi'], # Define dictionary
9 'age' : [34, 30, 16],
10 'city' : ['Sydney', 'Delhi', 'New york']}
11
12# Example usage 1:
13pandas_dataframe = pd.DataFrame(student_data)
14print(pandas_dataframe)
15 name age city # Dictionary keys become column names
160 Jack 34 Sydney
171 Riti 30 Delhi
182 Aadi 16 New york
19
20# Example usage 2:
21# Only select listed dictionary keys to dataframe columns:
22pandas_dataframe = pd.DataFrame(student_data, columns=['name', 'city'])
23print(pandas_dataframe)
24 name city
250 Jack Sydney
261 Riti Delhi
272 Aadi New york
28
29# Example usage 3:
30# Make pandas dataframe with keys as rownames:
31pandas_dataframe = pd.DataFrame.from_dict(student_data, orient='index')
32print(pandas_dataframe)
33 0 1 2
34name Jack Riti Aadi # Keys become rownames
35age 34 30 16
36city Sydney Delhi New york
1In [11]: pd.DataFrame(d.items()) # or list(d.items()) in python 3
2Out[11]:
3 0 1
40 2012-07-02 392
51 2012-07-06 392
62 2012-06-29 391
73 2012-06-28 391
8...
9
10In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
11Out[12]:
12 Date DateValue
130 2012-07-02 392
141 2012-07-06 392
152 2012-06-29 391
16