2017-03-07 51 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) 
相關問題