Add multiple columns to a data frame using Dictionary and zip()
Using Dict and zip() we can create a mapping of key values, which can be assigned to a new column name.
Python3
# importing pandas library import pandas as pd # creating and initializing a nested list students = [[ 'jackma' , 34 , 'Sydeny' , 'Australia' ], [ 'Ritika' , 30 , 'Delhi' , 'India' ], [ 'Vansh' , 31 , 'Delhi' , 'India' ], [ 'Nany' , 32 , 'Tokyo' , 'Japan' ], [ 'May' , 16 , 'New York' , 'US' ], [ 'Michael' , 17 , 'las vegas' , 'US' ]] # Create a DataFrame object df = pd.DataFrame(students, columns = [ 'Name' , 'Age' , 'City' , 'Country' ], index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ]) # creating 2 lists 'ids' and 'marks' ids = [ 11 , 12 , 13 , 14 , 15 , 16 ] marks = [ 85 , 41 , 77 , 57 , 20 , 95 , 96 ] # Creating columns 'ID' and 'Uni_marks' # using Dictionary and zip() df[ 'ID' ] = dict ( zip (ids, df[ 'Name' ])) df[ 'Uni_Marks' ] = dict ( zip (marks, df[ 'Name' ])) # Displaying the Data frame df |
Output :
Add multiple columns to dataframe in Pandas
In Pandas, we have the freedom to add columns in the data frame whenever needed. There are multiple ways to add columns to pandas dataframe.