Python 在熊猫中合并和减去 DataFrame 列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29832455/
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
Merging and subtracting DataFrame columns in pandas?
提问by user1566200
I have a pandas DataFrame, something like:
我有一个 Pandas DataFrame,类似于:
col1 col2 col3 col5
NaN 1 2 8
2 NaN 4 8
4 NaN 4 8
I want to do two things:
我想做两件事:
1) Merge columns 1 and 2:
1) 合并第 1 列和第 2 列:
newcol1 col3 col5
1 2 8
2 4 8
4 4 8
I have tried using .concat, but that just concatenates the rows. Doesn't seem like I can use standard +
operators with NaN values.
我试过使用 .concat,但这只是连接行。似乎我不能使用+
带有 NaN 值的标准运算符。
2) Subtract column 5 from new column 1 and column 3, so I end up with:
2) 从新的第 1 列和第 3 列中减去第 5 列,所以我最终得到:
newcol1 col3
-7 -6
-6 -4
-4 -4
Tried doing it this way:
尝试这样做:
dataframe[['newcol1', 'col2']] - dataframe['col5']
and
和
dataframe[['newcol1', 'col2']].subtract(dataframe['col5'])
but neither works.
但两者都不起作用。
回答by EdChum
In [58]:
df['newcol'] = df[['col1','col2']].sum(axis=1) - df['col5']
df['col3'] = df['col3'] - df['col5']
df
Out[58]:
col1 col2 col3 col5 newcol
0 NaN 1 -6 8 -7
1 2 NaN -4 8 -6
2 4 NaN -4 8 -4
You can then drop col1 and col2:
然后你可以删除 col1 和 col2:
In [59]:
df = df.drop(['col1','col2'],axis=1)
df
Out[59]:
col3 col5 newcol
0 -6 8 -7
1 -4 8 -6
2 -4 8 -4
回答by Alex Riley
To get the new column, you could use fillna
(or combine_first
):
要获取新列,您可以使用fillna
(或combine_first
):
df['newcol1'] = df.col1.fillna(df.col2)
Then for the subtraction, use sub
and specify axis=0
since we want to consider the row indices when matching up labels (not the column indices as is the default):
然后对于减法,使用sub
并指定,axis=0
因为我们要在匹配标签时考虑行索引(而不是默认的列索引):
>>> df[['newcol1', 'col3']].sub(df['col5'], axis=0)
newcol1 col3
0 -7 -6
1 -6 -4
2 -4 -4
回答by Zero
Here's one approach.
这是一种方法。
You could create newcol1
by sum(axis=1)
你可以newcol1
通过创建sum(axis=1)
In [256]: df['newcol1'] = df[['col1', 'col2']].sum(axis=1)
In [257]: df
Out[257]:
col1 col2 col3 col5 newcol1
0 NaN 1 2 8 1
1 2 NaN 4 8 2
2 4 NaN 4 8 4
Then use df.sub()
on axis=0
然后用df.sub()
在axis=0
In [258]: df[['newcol1', 'col3']].sub(df['col5'], axis=0)
Out[258]:
newcol1 col3
0 -7 -6
1 -6 -4
2 -4 -4