【问题标题】:concatenate all strings of one variable in python [duplicate]在python中连接一个变量的所有字符串[重复]
【发布时间】:2019-05-20 00:17:09
【问题描述】:

我有一个数据框,其中包含一个名为 var1 和 var2 的变量。有5个观察,如下:

var1 var2

1  hello there
2  my name is
3  john and 
4  i am 30 years 
5  old 

有没有办法将 var2 中的每个观察字符串连接成一个字符串?例如,创建一个字符串,将 var 中的观察结果连接起来:“你好,我的名字是 john,我 30 岁”

我在网上到处寻找将特定 var 的每个观察结果转换为一个字符串的方法,但找不到示例。

【问题讨论】:

  • 请先尝试搜索。

标签: python string pandas list string-concatenation


【解决方案1】:

是的,使用join

s = ' '.join(df['var2'])
print (s)
hello there my name is john and i am 30 years old

【讨论】:

  • 该死,你的速度很快! :)
  • @user5173426 确实,输入一行来重复问题很容易。
猜你喜欢
  • 2018-06-21
  • 2016-08-08
  • 1970-01-01
  • 2014-07-20
  • 2018-10-15
  • 2020-12-30
  • 1970-01-01
  • 2022-07-06
  • 2019-10-23
相关资源
最近更新 更多