python中有将两列数据合并为一列数据的函数么

python中想将dataframe类型的数据中两列(A、B)中的数据用“字符串连接”合并为一列。有什么函数么?比如
A B c
0 a 1 asd
1 a 2 fdf
2 b 1 dff
变为
d c
0 a1 asd
1 a2 fdf
2 b1 dff
谢谢

有, 要用apply函数。一种方式:
def my_test(a, b):

return a + b
df['value'] = df.apply(lambda row: my_test(row['A'], row['B']), axis=1)
apply完了产生一列新的series。注意axis=1 不能漏了 ,表示apply的方向是纵向
温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-08-01
假设数据存储在文件 test.txt中,程序如下(未经测试,大概是这么个意思)
lines=open(r'test.txt').readlines()

text=[]
for line in lines:
word=line.split()
thirdword=word[2].strip()
text.append(thirdword)
result=''.join(text)
print result
第2个回答  2019-08-01
thirdword=word[2].strip()
相似回答