简体   繁体   中英

Pandas cell value is a column name in another dataframe

I have two huge dataframes. For the sake of simplicity,

df1:
Item   Date   Demand   
A       01     Jan
A       02     Jan
A       03     Jan
B       01     Apr
B       02     Apr
B       03     Apr
C       01     Mar
C       02     Mar
C       03     Mar


df2:
Item   Date   Jan   Feb    Mar   Apr   May    Jun
 A     01     343   335    343   231   234    345
 A     02     340   331    344   230   230    343
 A     03     373   334    341   235   264    344
 B     01     345   456    455   453   612    342
 B     02     348   450    453   455   642    345
 B     03     346   459    452   454   632    342
 C     01     456   341    832   342   123    411
 C     02     452   340    836   341   423    413
 C     03     451   348    837   346   153    410

For df1 , I want to replace the Month name present in demand column with its value from df2 . For example, In the first row, Jan should be replaced by 343 which is the value in Jan column of df2 for Item A, Date 01

The final output should look like this -

    Item   Date   Demand   
    A       01     343
    A       02     340
    A       03     373
    B       01     453
    B       02     455
    B       03     454
    C       01     832
    C       02     836
    C       03     837

I tried different ways, but none of them worked. Please help me out. Thanks.

thushv89's answer is good! However, in a different situation you may want to just melt df2.

df2 = df2.melt(id_vars=['Item','Date'],
               value_vars=['Jan','Feb','Mar','Apr','May','Jun'],
               var_name='Month',
               value_name='Demand')

Output:

   Item  Date Month  Demand
0     A     1   Jan     343
1     A     2   Jan     340
2     A     3   Jan     373
3     B     1   Jan     345
4     B     2   Jan     348
5     B     3   Jan     346
6     C     1   Jan     456
7     C     2   Jan     452
8     C     3   Jan     451
9     A     1   Feb     335
10    A     2   Feb     331
11    A     3   Feb     334
12    B     1   Feb     456
13    B     2   Feb     450
14    B     3   Feb     459
...

How about this?

df2 = df2.set_index(['Item', 'Date'], drop=True)
df["Demand"] = df.apply(lambda x: df2.loc[(x.Item,x.Date), x.Demand], axis=1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM