Subtract one dataframe from another excluding the first column Pandas

后端 未结 3 1630
说谎
说谎 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.

    0 讨论(0)
  • 2021-01-27 17:53

    you can also use join and iloc:

    df_tot.iloc[:,:1].join(df_tot.iloc[:,1:]-df_nap.iloc[:,1:])
    

    but this implies to have the same order of columns and 'wavelength' being the first one

    0 讨论(0)
  • 2021-01-27 17:59

    Simply subtract the entire DataFrames, then reassign the desired values to the Wavelength column.

    result = df_tot - df_nap
    result['Wavelength'] = df_tot['Wavelength']
    

    For example,

    import numpy as np
    import pandas as pd
    
    df_tot = pd.DataFrame(np.random.randint(10, size=(3,4)), columns=list('ABCD'))
    df_nap = pd.DataFrame(np.random.randint(10, size=(3,4)), columns=list('ABCD'))
    # df_tot['A'] = df_nap['A']   # using column A as the "Wavelength" column
    
    result = df_tot - df_nap
    result['A'] = df_tot['A']
    

    Alternatively, or if Wavelength column were not numeric, you could subtract everything except the Wavelength, then reassign that column:

    result = df_tot.drop('Wavelength', axis=1) - df_nap.drop('Wavelength', axis=1)
    result['Wavelength'] = df_tot['Wavelength']
    
    0 讨论(0)
提交回复
热议问题