【问题标题】:Join(Combine) two series in pandas在 pandas 中加入(组合)两个系列
【发布时间】:2018-07-07 05:35:34
【问题描述】:

我想加入/合并数据框中的两列。

数据

import pandas as pd
dat = pd.DataFrame({'A' : [1, 2, 3], 'B' : [4, 5, 6]})

期望的输出

14
25
36

【问题讨论】:

标签: python pandas


【解决方案1】:

applyjoin 一起使用

dat.astype(str).apply(''.join,1)
Out[210]: 
0    14
1    25
2    36
dtype: object

或者(PS并不总是有效)

dat.A*10+dat.B
Out[211]: 
0    14
1    25
2    36
dtype: int64

【讨论】:

  • 谢谢!您如何定义第一个解决方案中的列?
  • @Vedda 使用dat.astype(str).apply(''.join,1).to_frame('Yourname') 或者你需要? dat[['A','B']].astype(str).apply(''.join,1)
猜你喜欢
  • 1970-01-01
  • 2014-11-16
  • 2012-06-13
  • 2020-11-13
  • 2016-11-24
  • 2018-12-20
  • 2023-01-02
  • 2013-09-18
相关资源
最近更新 更多