Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map dataFrame values to another DataFrame

Tags:

python

pandas

I have these two dataFrames

data1 = [[1,'A'],[2,'B'],[3,'C'],[4,'D'],[5,'E']]
data2 = [1,1,1,1,2,5,4,3]
df1 = pd.DataFrame(data1,columns = ['one','two'])
df2 = pd.DataFrame(data2,columns = ['one'])

I want to map all values of df2 of column one with df1 of column two. In simple terms i want to use df1 as a dictionary. I want output like this for df2

   one
0    A
1    A
2    A
3    A
4    B
5    E
6    D
7    C
  

I am doing this

df2['one']= df2['one'].apply(lambda x: df1.two[df1.one == x])

Which gives me output

   one
0    A
1    A
2    A
3    A
4  NaN
5  NaN
6  NaN
7  NaN

All A is correct but why latter all are NaN?

like image 448
Ajay Chinni Avatar asked Feb 17 '26 23:02

Ajay Chinni


2 Answers

Try this, much better syntax and functionality over using apply with a lambda function:

df2['one'].map(df1.set_index('one')['two'])

Output:

0    A
1    A
2    A
3    A
4    B
5    E
6    D
7    C
Name: one, dtype: object

Why your method doesn't work.... Look at the output of :

df2['one'].apply(lambda x: df1.two[df1.one == x])

Output:

     0    1    2    3    4
0    A  NaN  NaN  NaN  NaN
1    A  NaN  NaN  NaN  NaN
2    A  NaN  NaN  NaN  NaN
3    A  NaN  NaN  NaN  NaN
4  NaN    B  NaN  NaN  NaN
5  NaN  NaN  NaN  NaN    E
6  NaN  NaN  NaN    D  NaN
7  NaN  NaN    C  NaN  NaN

Because of index alignment in pandas only the first column, 0. get assigned. Here, you are using pd.Series.apply where you are applying the lambda function over the elements of a series and assigning it back to a dataFrame causing your mal-alignment issues.

like image 189
Scott Boston Avatar answered Feb 19 '26 13:02

Scott Boston


dict df1 columns and map to df2.

df2.one=df2.one.map(dict(zip(df1.one,df1.two)))

  one
0   A
1   A
2   A
3   A
4   B
5   E
6   D
7   C
like image 37
wwnde Avatar answered Feb 19 '26 13:02

wwnde



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!