Pandas - 添加列,匹配索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44505738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas - adding columns, matching on index
提问by keynesiancross
I'm trying to figure out if Panda's, when adding two series together, automatically matches on index or if it simply adds by element position. If its just by position, is there a way to get it to add on index? I've looked at merging, but I'm not very clear if the key in this case can be the index of both...
我试图弄清楚 Panda 是否在将两个系列加在一起时自动匹配索引,或者它是否只是按元素位置添加。如果只是按位置,有没有办法让它添加到索引上?我看过合并,但我不太清楚这种情况下的键是否可以是两者的索引......
For example, if I have do DFs:
例如,如果我有做 DF:
df1 = index value
0 10
1 12
2 15
4 20
df2 = index value
0 10
1 10
3 10
4 10
and I want to add df1[total] = df1[value] + df2[value] =
我想添加 df1[total] = df1[value] + df2[value] =
df1 = index value
0 20
1 22
2 15
3 10
4 30
Thanks for your help in advance!
提前感谢您的帮助!
采纳答案by Scott Boston
Because of the intrinsic data alignmentin pandas, you can use add
with fill_value=0
and it will sum these two series based on index alignment.
由于 Pandas 中的内在数据对齐,您可以使用add
withfill_value=0
并且它会根据索引对齐将这两个系列相加。
df1.add(df2,fill_value=0)
Input:
输入:
df1 = pd.Series([10]*4,index=[0,1,3,4])
df2 = pd.Series([10,12,15,20], index=[0,1,2,4])
df1.add(df2,fill_value=0)
Output:
输出:
0 20.0
1 22.0
2 15.0
3 10.0
4 30.0
dtype: float64
回答by Gene Burinsky
Just do this:
只需这样做:
pd.concat([df1,df2], axis=1).sum(axis=1)
pd.concat
will merge the 2(or more) frames and match based on index. sum(axis=1)
just sums across the rows.
pd.concat
将合并 2 个(或更多)帧并根据索引进行匹配。sum(axis=1)
只是跨行求和。
Here's the working example:
这是工作示例:
#create the example data
df1 = pd.DataFrame({'index':[0,1,2,4],'value':[10,12,15,20]}).set_index('index')
df2 = pd.DataFrame({'index':[0,1,3,4],'value':[10,10,10,10]}).set_index('index')
The above will give you:
以上将为您提供:
In [7]: pd.concat([df1,df2],axis=1).sum(axis=1)
Out[7]:
index
0 20.0
1 22.0
2 15.0
3 10.0
4 30.0
dtype: float64