简体   繁体   中英

Replacing row data in Dataframe whose oringial values are columns from another dataframe, with the values from this 2nd dataframe

If I have 1 dataframe df_1 with integer IDs as cols and date as index:

             12345           6789        2222        4444
01.01.2021   categ_id_1      categ_id_2  categ_id_3  categ_id_4

and 2nd dataframe df_2 with the categ_ids, from df_1, as columns and date as index again:

             categ_id_1      categ_id_2  categ_id_3  categ_id_4 etc
01.01.2021   0.0034          0.045       0.08        0.64

Am trying to obtain df_3:

             12345       6789   2222  4444
01.01.2021   0.0034      0.045  0.08  0.64

This looks straightforward, but the different nature of the 2 dataframes.. (lambda function/expressn?)

If anyone has any pointers.. Thanks

Clunkily resolved by (repro-typo's apart):

df_1.columns.set_names("integ_ids",inplace=True)
df_1.index.set_names("date",inplace=True)

df_2.columns.set_names("categ_ids",inplace=True)
df_2.index.set_names("date",inplace=True)

df_1_unpacked=df_1.unstack().to_frame().rename(columns={0:"categ_ids"})
df_2_unpacked=df_2.unstack().to_frame().rename(columns={0:"df2_vals"})

df_temp_1=df_1_unpacked.join(df_2_unpacked,on=['categ_ids','date'])

df_temp_2=df_temp_1.drop('categ_ids',axis=1)
df_3=df_temp_2.unstack(0).droplevel(0,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