Skip to content Skip to sidebar Skip to footer

How To Substract Values In DataFrames Omiting Some Solumns

I created two dataFrames and I want to subtract their values omitting two first columns in the first DataFrame. df = pd.DataFrame({'sys':[23,24,27,30],'dis': [0.8, 0.8, 1.0,1.0], '

Solution 1:

Maybe, this is what you are looking for?

>>> df
            England  Germany      US
0  23  0.8      0.0      0.0   500.0
1  24  0.8   1000.0      0.0     0.0
2  27  1.0      0.0      0.0  1500.0
3  30  1.0      0.0   2000.0     0.0
>>> infile_slice
   Benchmark
0     3.3199
1    -4.0135
2    -4.9794
3    -3.1766
>>> df.iloc[:, 4:] = df.iloc[:, 4:].sub(infile_slice['Benchmark'].values,axis=0)
>>> df
            England  Germany         US
0  23  0.8      0.0      0.0   496.6801
1  24  0.8   1000.0      0.0     4.0135
2  27  1.0      0.0      0.0  1504.9794
3  30  1.0      0.0   2000.0     3.1766
>>> 

Solution 2:

You could use iloc as follows:

df_0_2 = df.iloc[:,0:2]      # First 2 columns
df_2_end = df.iloc[:,2:].sub(infile_slice['Benchmark'].values, axis=0)

pd.concat([df_0_2, df_2_end], axis=1)

              England    Germany         US
0  23  0.8    -3.3199    -3.3199   496.6801
1  24  0.8  1004.0135     4.0135     4.0135
2  27  1.0     4.9794     4.9794  1504.9794
3  30  1.0     3.1766  2003.1766     3.1766

Post a Comment for "How To Substract Values In DataFrames Omiting Some Solumns"