Creating a pandas dataframe with dictionary data.
A dictionary containing lists as values can directly passed into pd.Dataframe method to create a new dataframe using the passed data. The keys in the dictionary will be used as the column headers and values in the list will be used as row values.
# importing pandas
import pandas as pd
# animal_data dictionary
animal_data = {
"Name": ["Cat", "Dog", "Cow"],
"Speed": [15, 12, 10],
"Sound": ["Meow", "Woof", "Mooo"],
}
#creating a dataframe using the animal_data dictionary
animal_df = pd.DataFrame(animal_data)
#printing animal_df
print("animal_df \n", animal_df)
animal_df
Name Speed Sound
0 Cat 15 Meow
1 Dog 12 Woof
2 Cow 10 Mooo
Comments
Post a Comment
Oof!