作为对象构造函数的一部分,我想逐个减去两个熊猫数据帧的值:
self.dfload=pd.read_csv(self.name +'/' + 'load.csv')
self.dfload.set_index('snapshot', inplace=True)
load_1 load_2 load_3 load_4
snapshot
2018-01-01 00:00:00 68.248569 91.998188 64.988923 139.535086
2018-01-01 00:15:00 138.274243 127.186259 80.769227 33.509007
2018-01-01 00:30:00 129.824298 56.706114 75.234845 138.610287
2018-01-01 00:45:00 51.754610 45.703056 73.060490 36.913774
2018-01-01 01:00:00 52.129775 139.315283 67.093788 60.488806
和
self.dfsupply=pd.read_csv(self.name + '/' + 'supply.csv')
self.dfsupply.set_index('snapshot', inplace=True)
supply_1 supply_2 supply_3 supply_4
snapshot
2018-01-01 00:00:00 28.448017 45.383377 56.626144 40.044848
2018-01-01 00:15:00 37.534878 29.094980 67.722537 15.002448
2018-01-01 00:30:00 46.163805 28.324557 26.322953 23.250904
2018-01-01 00:45:00 48.192774 55.049855 72.872200 21.602035
2018-01-01 01:00:00 60.499436 53.698329 74.674572 42.425620
作者
self.dfresidualLoad=self.dfsupply.subtract(self.dfload, axis='column')
每个元素和两个df的串联结果为NaN:
load_1 load_2 load_3 ... supply_1 supply_2 ...
snapshot
2018-01-01 00:00:00 NaN NaN NaN NaN NaN NaN
.
.
彼此减去单列没有问题。不幸的是,这不是理想的解决方案,因为我想保持未定义的列数。
答案 0 :(得分:2)
如果两个DataFrame中的列名称和索引相同,则减去第二个DataFrame
创建的numpy数组:
self.dfresidualLoad=self.dfsupply - self.dfload.values
或如果列名的位置匹配,则使用rename
列:
d = dict(zip(dfload.columns, dfsupply.columns))
df = dfsupply.subtract(dfload.rename(columns=d), axis='column')
print (df)
supply_1 supply_2 supply_3 supply_4
2018-01-01 00:00:00 -39.800552 -46.614811 -8.362779 -99.490238
2018-01-01 00:15:00 -100.739365 -98.091279 -13.046690 -18.506559
2018-01-01 00:30:00 -83.660493 -28.381557 -48.911892 -115.359383
2018-01-01 00:45:00 -3.561836 9.346799 -0.188290 -15.311739
2018-01-01 01:00:00 8.369661 -85.616954 7.580784 -18.063186