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 columnsdf_2_end=df.iloc[:,2:].sub(infile_slice['Benchmark'].values,axis=0)pd.concat([df_0_2,df_2_end],axis=1)EnglandGermanyUS0230.8-3.3199-3.3199496.68011240.81004.0135 4.01354.01352271.04.97944.97941504.97943301.03.17662003.1766 3.1766
Post a Comment for "How To Substract Values In Dataframes Omiting Some Solumns"