# Importing pandas package
import pandas as pd
# Dictionary having students data
students = {'Name':['Alvin', 'Alex', 'Peter'],
'Age':[21, 22, 19]}
# Convert the dictionary into DataFrame
dataframe = pd.DataFrame(students)
# Print the data before adding column
print("Data before adding column...")
print(dataframe)
print()
# Dictionary with key value from an existing DataFrame
# and, values of the new column to be added
address = {'B.tech':'Alvin', 'MCA':'Alvin', 'B.E.':'Peter'}
# Add the Dictionary to the DataFrame
dataframe['Course'] = address
# Print the data after adding column
print("Data after adding column ...")
print(dataframe)
print()
Output:
Data before adding column...
Name Age
0 Alvin 21
1 Alex 22
2 Peter 19
Data after adding column ...
Name Age Course
0 Alvin 21 B.tech
1 Alex 22 MCA
2 Peter 19 B.E.
Example:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer