【发布时间】:2017-10-25 08:21:38
【问题描述】:
我正在使用 pandas 数据框并尝试将多个字符串和数字连接成一个字符串。
这行得通
df1 = pd.DataFrame({'Col1': ['a', 'b', 'c'], 'Col2': ['a', 'b', 'c']})
df1.apply(lambda x: ', '.join(x), axis=1)
0 a, a
1 b, b
2 c, c
我怎样才能像 df1 一样进行这项工作?
df2 = pd.DataFrame({'Col1': ['a', 'b', 1], 'Col2': ['a', 'b', 1]})
df2.apply(lambda x: ', '.join(x), axis=1)
TypeError: ('sequence item 0: expected str instance, int found', 'occurred at index 2')
【问题讨论】:
-
尝试将
lambda x: ', '.join(x)更改为lambda x: ', '.join(str(x))
标签: python string pandas concatenation