Pandas - 合并两列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51635290/
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 - combine two columns
提问by JesusMonroe
I have 2 columns, which we'll call x
and y
. I want to create a new column called xy
:
我有 2 列,我们将其称为x
和y
。我想创建一个名为的新列xy
:
x y xy
1 1
2 2
4 4
8 8
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.
不应该有任何冲突的值,但如果有,y 优先。如果它使解决方案更加容易,你可以假设x
总是会NaN
在那里y
有一个值。
回答by SuperStew
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']
回答by YOBEN_S
Notice your column type right now is string not numeric anymore
注意你的列类型现在是字符串而不是数字了
df = df.apply(lambda x : pd.to_numeric(x, errors='coerce'))
df['xy'] = df.sum(1)
More
更多的
df['xy'] =df[['x','y']].astype(str).apply(''.join,1)
#df[['x','y']].astype(str).apply(''.join,1)
Out[655]:
0 1.0
1 2.0
2
3 4.0
4 8.0
dtype: object
回答by jpp
You can also use NumPy:
您还可以使用 NumPy:
import pandas as pd, numpy as np
df = pd.DataFrame({'x': [1, 2, np.nan, np.nan],
'y': [np.nan, np.nan, 4, 8]})
arr = df.values
df['xy'] = arr[~np.isnan(arr)].astype(int)
print(df)
x y xy
0 1.0 NaN 1
1 2.0 NaN 2
2 NaN 4.0 4
3 NaN 8.0 8