Mapping external values to a dataframe means using different sets of values to add to that dataframe by keeping the keys of the external dictionary as same as the one column of that dataframe. To add external values to dataframe, we use a dictionary that has keys and values which we want to add to the dataframe. By adding external values in the dataframe one column will be added to the current dataframe. We can also map or combine one dataframe to other dataframe with the help of pandas.
Method #1: Using mapping function
By using the mapping function we can add one more column to an existing dataframe. Just keep in mind that no key values will be repeated it will make the data inconsistent.
Python3
import pandas as pd
initial_data = { 'First_name' : [ 'Ram' , 'Mohan' , 'Tina' , 'Jeetu' , 'Meera' ],
'Last_name' : [ 'Kumar' , 'Sharma' , 'Ali' , 'Gandhi' , 'Kumari' ],
'Age' : [ 42 , 52 , 36 , 21 , 23 ],
'City' : [ 'Mumbai' , 'Noida' , 'Pune' , 'Delhi' , 'Bihar' ]}
df = pd.DataFrame(initial_data, columns = [ 'First_name' , 'Last_name' ,
'Age' , 'City' ])
new_data = { "Ram" : "B.Com" ,
"Mohan" : "IAS" ,
"Tina" : "LLB" ,
"Jeetu" : "B.Tech" ,
"Meera" : "MBBS" }
df[ "Qualification" ] = df[ "First_name" ]. map (new_data)
print (df)
|
Output:
First_name Last_name Age City Qualification
0 Ram Kumar 42 Mumbai B.Com
1 Mohan Sharma 52 Noida IAS
2 Tina Ali 36 Pune LLB
3 Jeetu Gandhi 21 Delhi B.Tech
4 Meera Kumari 23 Bihar MBBS
Method #2: Using replace
function
In this method, we can add or replace some of the values of the dataframe with some defined external values.
Python3
import pandas as pd
initial_data = { 'First_name' : [ 'Ram' , 'Mohan' , 'Tina' , 'Jeetu' , 'Meera' ],
'Last_name' : [ 'Kumar' , 'Sharma' , 'Ali' , 'Gandhi' , 'Kumari' ],
'Age' : [ 42 , 52 , 36 , 21 , 23 ],
'City' : [ 'Mumbai' , 'Noida' , 'Pune' , 'Delhi' , 'Bihar' ]}
df = pd.DataFrame(initial_data, columns = [ 'First_name' , 'Last_name' ,
'Age' , 'City' ])
new_data = { "Ram" : "Shyam" ,
"Tina" : "Riya" ,
"Jeetu" : "Jitender" }
print (df, end = "\n\n" )
df = df.replace({ "First_name" :new_data})
print (df)
|
Output:
First_name Last_name Age City
0 Ram Kumar 42 Mumbai
1 Mohan Sharma 52 Noida
2 Tina Ali 36 Pune
3 Jeetu Gandhi 21 Delhi
4 Meera Kumari 23 Bihar
First_name Last_name Age City
0 Shyam Kumar 42 Mumbai
1 Mohan Sharma 52 Noida
2 Riya Ali 36 Pune
3 Jitender Gandhi 21 Delhi
4 Meera Kumari 23 Bihar
Method #3: Using update
function
In this method we can update the dataframe values by using index values we can change the value of columns by external data.
Python3
import pandas as pd
initial_data = { 'First_name' : [ 'Ram' , 'Mohan' , 'Tina' , 'Jeetu' , 'Meera' ],
'Last_name' : [ 'Kumar' , 'Sharma' , 'Ali' , 'Gandhi' , 'Kumari' ],
'Age' : [ 42 , 52 , 36 , 21 , 23 ],
'City' : [ 'Mumbai' , 'Noida' , 'Pune' , 'Delhi' , 'Bihar' ]}
df = pd.DataFrame(initial_data, columns = [ 'First_name' , 'Last_name' ,
'Age' , 'City' ])
new_data = { 0 : "Shyam" ,
2 : "Riya" ,
3 : "Jitender" }
df[ "First_name" ].update(pd.Series(new_data))
print (df)
|
Output:
First_name Last_name Age City
0 Shyam Kumar 42 Mumbai
1 Mohan Sharma 52 Noida
2 Riya Ali 36 Pune
3 Jitender Gandhi 21 Delhi
4 Meera Kumari 23 Bihar
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
31 Jul, 2023
Like Article
Save Article