简体   繁体   English

减去两个同名不同索引的数据帧

[英]Subtract two dataframe with the same name different index

There's two dataframes, all the colnames are the same but the index is different.有两个数据框,所有列名都相同但索引不同。 I've tried several ways but I've been getting NaN .我尝试了几种方法,但我一直在得到NaN I'm guessing it is because it's trying to match the index but I am stuck.我猜这是因为它试图匹配索引但我被卡住了。

I've tried:我试过了:

df1.ix['Norm']-df2.ix['Norm']
df1 - df2.ix['Norm']
df1.astype(float) - df2.astype(float)

would like something like this想要这样的东西

df1 - df2[df1.column.names]

When you don't want index-aligned arithmetic, get rid of the indexes.当你不想要索引对齐的算术时,去掉索引。 You can subtract the numpy arrays to the same effect:您可以减去 numpy 数组以达到相同的效果:

(df1.values - df2[df1.column.names].values)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM