Answers for "Dictionary rows in pandas dataframe"

3

python how to create a pandas dataframe from a dictionary

# Basic syntax:
import pandas as pd
pandas_dataframe = pd.DataFrame(dictionary)
# Note, with this command, the keys become the column names 

# Create dictionary:
import pandas as pd
student_data = {'name' : ['Jack', 'Riti', 'Aadi'], # Define dictionary
    	   	    'age' : [34, 30, 16],
    		    'city' : ['Sydney', 'Delhi', 'New york']}

# Example usage 1:
pandas_dataframe = pd.DataFrame(student_data) 
print(pandas_dataframe)
	name	age	city	# Dictionary keys become column names
0	Jack	34	Sydney
1	Riti	30	Delhi
2	Aadi	16	New york

# Example usage 2:
# Only select listed dictionary keys to dataframe columns:
pandas_dataframe = pd.DataFrame(student_data, columns=['name', 'city'])
print(pandas_dataframe)
	name	city
0	Jack	Sydney
1	Riti	Delhi
2	Aadi	New york

# Example usage 3:
# Make pandas dataframe with keys as rownames:
pandas_dataframe = pd.DataFrame.from_dict(student_data, orient='index') 
print(pandas_dataframe)
           0      1         2
name    Jack   Riti      Aadi # Keys become rownames
age       34     30        16
city  Sydney  Delhi  New york
Posted by: Guest on October-03-2020
0

pandas to dictionary

df.to_dict('records')
Posted by: Guest on August-06-2020

Code answers related to "Dictionary rows in pandas dataframe"

Python Answers by Framework

Browse Popular Code Answers by Language