2017-03-07 46 views
1

我有带数据框的int类型列。将数字列与数据框结合起来

我想创建一个新列,它将保存行的文本表示。

df['text'] = df[['project', 'from', 'to', 'score']].apply(lambda x: '_'.join(x), axis=1) 

我收到以下错误TypeError: ('sequence item 1: expected string or Unicode, int found', u'occurred at index 0')

如何,我可以用我的文本生成函数添加casting to string

回答

2

尝试投地strastype

df['text'] = df[['project', 'from', 'to', 'score']] 
       .apply(lambda x: '_'.join(x.astype(str)), axis=1) 

或者:

df['text'] = df[['project', 'from', 'to', 'score']] 
       .astype(str) 
       .apply(lambda x: '_'.join(x), axis=1) 
相关问题