在大 pandas 中合并和减去DataFrame列? [英] Merging and subtracting DataFrame columns in pandas?

查看:153
本文介绍了在大 pandas 中合并和减去DataFrame列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大熊猫DataFrame,如:

  col1 col2 col3 col5 
NaN 1 2 8
2 NaN 4 8
4 NaN 4 8

我想做两件事情:



1)合并列1和2:

  newcol1 col3 col5 
1 2 8
2 4 8
4 4 8

我已经尝试使用.concat,但只是连接这些行。看起来不像我可以使用具有NaN值的标准 + 运算符。



2)从第5列中删除新的第1列和第3列,所以我最终得到:

  newcol1 col3 
-7 -6
-6 -4
-4 -4

尝试这样做:

  dataframe [['newcol1','col2']]  -  dataframe ['col5'] 

  dataframe [['newcol1' ,'col2']]。subtract(dataframe ['col5'])

/ p>

解决方案

要获得新列,您可以使用 fillna (或 combine_first ):

  df ['newcol1'] = df.col1。 fillna(df.col2)

然后对于减法,使用 sub 并指定 axis = 0 ,因为我们想在匹配标签时考虑行索引(不是列索引的默认值):

 >>> df [['newcol1','col3']]。sub(df ['col5'],axis = 0)
newcol1 col3
0 -7 -6
1 -6 -4
2 -4 -4


I have a pandas DataFrame, something like:

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:

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.

2) Subtract column 5 from new column 1 and column 3, so I end up with:

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.

解决方案

To get the new column, you could use fillna (or 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):

>>> df[['newcol1', 'col3']].sub(df['col5'], axis=0)
   newcol1  col3
0       -7    -6
1       -6    -4
2       -4    -4

这篇关于在大 pandas 中合并和减去DataFrame列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆