Reputation: 625
import pandas as pd
df = pd.DataFrame({"A":["foo", "foo", "foo", "bar","Panda", "Panda", "Zootopia", "Zootopia"],"B":[0,1,1,1,0,1,1,1]})
df1 = pd.DataFrame({"A":["foo", "foo", "foo", "bar","Panda", "Panda", "Zootopia", "Zootopia","Toy Story"]})
If column A in df and df1 matches then replace df1 column A by df column B value also count number of values not replaced and take a list of them.
Expected Output
A
0 0.0
1 1.0
2 1.0
3 1.0
4 0.0
5 1.0
6 1.0
7 1.0
8 Toy Story
Not Replaced Count: 1
Not Replaced DataFrame:
0 A
Toy Story
Upvotes: 2
Views: 83
Reputation: 1704
Here is one solution:
num = len(df['A'])
if all(df1['A'][:num]==df['A'][:num]):
df1['A'][:num] = df['B']
Output for df1:
A
0 0
1 1
2 1
3 1
4 0
5 1
6 1
7 1
8 Toy Story
If you did not want to alter df1, you could make a copy of it and run the code:
num = len(df['A'])
df1_copy = df1.copy()
if all(df1_copy['A'][:num]==df['A'][:num]):
df1_copy['A'][:num] = df['B']
Output for df1:
A
0 foo
1 foo
2 foo
3 bar
4 Panda
5 Panda
6 Zootopia
7 Zootopia
8 Toy Story
Output for df1_copy:
A
0 0
1 1
2 1
3 1
4 0
5 1
6 1
7 1
8 Toy Story
Upvotes: 1