Subtract one dataframe from another excluding the first column Pandas

后端 未结 3 1679
说谎
说谎 2021-01-27 17:22

I have to dataframes with the same columns. My task should be to subtract the df_tot from df_nap without touching the first column (\'A\'). What is the easiest solution for it?<

3条回答
  •  野性不改
    2021-01-27 17:51

    Set the common index for both dataframes before using pd.DataFrame.sub:

    df_tot = df_tot.set_index('Wavelength')
    df_nap = df_nap.set_index('Wavelength')
    
    res = df_tot.sub(df_nap)
    

    If you require 'Wavelength' as a series rather than an index, you can call reset_index on the result:

    res = res.reset_index()
    

    However, there are certain benefits attached to storing a unique row-identifier as an index rather than a series. For example, more efficient lookup and merge functionality.

提交回复
热议问题