pandas -结合两列 [英] Pandas - combine two columns
本文介绍了 pandas -结合两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2列,我们将其称为 x
和 y
.我想创建一个名为 xy
的新列:
I have 2 columns, which we'll call x
and y
. I want to create a new column called xy
:
x y xy
1 1
2 2
4 4
8 8
不应有任何冲突的值,但如果存在,则y优先.如果使解决方案更简单,则可以假定 x
始终为 NaN
,其中 y
具有值.
There shouldn't be any conflicting values, but if there are, y takes precedence. If it makes the solution easier, you can assume that x
will always be NaN
where y
has a value.
推荐答案
如果您的示例正确,则可能很简单
it could be quite simple if your example is accurate
df.fillna(0) #if the blanks are nan will need this line first
df['xy']=df['x']+df['y']
这篇关于 pandas -结合两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文