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# 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