There are 2 dataframes, the 1st dataframe contains 3 columns emp_id,emp_name and old email_id. While the 2nd dataframe contains 2 columns emp_id and new email_id.
Task is to, lookup 2nd dataframe and replace certain values in the 1st dataframe.
Input Dataframe:
import pandas as pd
data = {
'emp_id': [111, 222, 333, 444, 555],
'emp_name': ['Sam','Joe','Jake','Rob','Matt'],
'email_id': ['one@gmail.com','two@gmail.com','three@gmail.com','four@gmail.com','five@gmail.com']
}
data = pd.DataFrame(data)
Current Output:
emp_id emp_name email_id
0 111 Sam one@gmail.com
1 222 Joe two@gmail.com
2 333 Jake three@gmail.com
3 444 Rob four@gmailcom
4 555 Matt five@gmail.com
#replace certain values with new values by looking up another dataframe
data1 = {
'emp_id': [111, 333, 555],
'email_id': ['one@yahoo.com','three@yahoo.com','five@yahoo.com']
}
data1 = pd.DataFrame(data1)
Desired Output:
data =
emp_id emp_name email_id
0 111 Sam one@yahoo.com
1 222 Joe two@gmail.com
2 333 Jake three@yahoo.com
3 444 Rob four@gmailcom
4 555 Matt five@yahoo.com
Original data contains 50k+ rows so merging them doesn't seem like the correct option. Any help would be appreciated.
Cheers !
Let us try update
out = data.set_index('emp_id')
out.update(data1.set_index('emp_id')[['email_id']])
out.reset_index(inplace=True)
out
emp_id email_id
0 111 one@yahoo.com
1 222 two@gmail.com
2 333 three@yahoo.com
3 444 four@gmail.com
4 555 five@yahoo.com
The solution I will provide makes use of Panda's Boolean Indexing .
# Compare the column of interest of the DataFrames. Returns a Pandas Series with True/False based on the inequality operation.
difference = (data1['email_id'] != data2['email_id'])
# Get the indexes of the different rows, i.e the rows that hold True for the inequality.
indexes_to_be_changed = data1[difference].index
# Replace the rows of the first DataFrame with the rows of the second DataFrame.
data1.iloc[indexes_to_be_changed] = data2.iloc[indexes_to_be_changed]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.