Following the questions: Python Pandas Only Compare Identically Labeled DataFrame Objects & How to create a new data frame based on conditions from another data frame.
I am trying to create a column in an already existing dataframe df1 with values conditioned on another already existing dataframe df2 with different index and columns.
In df2 ['Home'] cities have a ['Home_ranking']. I need to match the ['Home_ranking'] in df1 with those of df2, the condition being that ['Home'] cities should match. For instance, city B is the first ['Home'] city in df1, I need to get it's ranking in df2, which is 2. So that the first ['Home_ranking'] in df1 is 2 and so forth.
df1 has 50 rows and more columns and df2 only has 20 rows and 4 columns.
>>> print(df1)
Home Away Home_ranking Away_ranking [other columns..]
0 city B city C 0 0
1 city D city D 0 0
2 city B city D 0 0
3 city A city A 0 0
>>> print(df2)
Home Away Home_ranking Away_ranking
0 city A city B 12 5
1 city D city C 4 7
2 city F city A 1 9
3 city B city D 2 8
Expected result
>>> print(df1)
Home Away Home_ranking Away_ranking
0 city B city C 2 7
1 city D city D 4 8
2 city B city D 2 8
3 city A city A 12 9
I have tried many things suggested in the previous questions, but none of the seem to work for me. I have also tried:
for i in df1.Home :
if df1.Home == df2.Home :
df1['Home_ranking'][i] = df2.Home_ranking[i]
yields "ValueError: Can only compare identically-labeled Series objects"
df1['Home_ranking'][i] = df2.loc[(df2['Home'] == df1['Home'][i])].Home_ranking
yields "KeyError: 'city B' I am lost, any help welcome.