Example 1: pandas dataframe from dict
data = {'col_1': [3, 2, 1, 0], 'col_2': ['a', 'b', 'c', 'd']}
pd.DataFrame.from_dict(data)
Example 2: pandas dataframe from dict
>>> 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
Example 3: convert dict to dataframe
pd.DataFrame.from_dict(data, orient='index').T
Example 4: python dictionary
dict1 = {'color': 'blue', 'shape': 'square', 'volume':40}
dict2 = {'color': 'red', 'edges': 4, 'perimeter':15}
dict1['area'] = 25
dict2['perimeter'] = 20
print(dict1['shape'])
dict1.get('false_key')
dict1.get('false_key', "key not found")
dict1.pop('volume')
dict1.update(dict2)
dict1
dict1.values()
dict1.keys()
dict1.items()
Example 5: python how to create a pandas dataframe from a dictionary
import pandas as pd
pandas_dataframe = pd.DataFrame(dictionary)
import pandas as pd
student_data = {'name' : ['Jack', 'Riti', 'Aadi'],
'age' : [34, 30, 16],
'city' : ['Sydney', 'Delhi', 'New york']}
pandas_dataframe = pd.DataFrame(student_data)
print(pandas_dataframe)
name age city
0 Jack 34 Sydney
1 Riti 30 Delhi
2 Aadi 16 New york
pandas_dataframe = pd.DataFrame(student_data, columns=['name', 'city'])
print(pandas_dataframe)
name city
0 Jack Sydney
1 Riti Delhi
2 Aadi New york
pandas_dataframe = pd.DataFrame.from_dict(student_data, orient='index')
print(pandas_dataframe)
0 1 2
name Jack Riti Aadi
age 34 30 16
city Sydney Delhi New york
Example 6: dicts python
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
print(x)
---------------------------------------------------------------------------
Mustang