2017-02-13 69 views
1

我的數據框的樣子字符串數組判刑數據幀大熊貓

Phrase           Sentiment 
[seri, escapad, demonstr, adag, good, goos]   1 
[seri, escapad, demonstr, adag, good, goos]   2 

當我使用命令df.dtypes

Phrase  object 
Sentiment  int64 
dtype: object 

我需要得到這樣一個數據幀:

Phrase           Sentiment 
seri escapad demonstr adag good goos   1 
seri escapad demonstr adag good bad    2 

我試過這個代碼

df['Phrase'] = df[df.Phrase.map(lambda x: ','.join(x))] 

我應該做的id

+0

你好,你可以將列表轉換成字符串...但是..你想用DF做什麼? – Ika8

+0

@ Ika8情緒分析 –

回答

2

差不多吧 - 你的df[一個額外層。試試這個:

df['Phrase'] = df.Phrase.map(lambda x: ' '.join(x)) 

您已經返回要用來替換Phrase列,但隨後你被它的價值,這是產生錯誤試圖索引。

1

我覺得你很親密。下面一行應該做的伎倆:

df['Phrase'] = df['Phrase'].map(lambda x: ' '.join(x)) 
1

使用 ' '不是','

你可以不用拉姆達這樣的:

df['Phrase'] = df.Phrase.apply(', '.join) 

使用lambda:

df['Phrase'] = df.Phrase.apply(lambda x: ', '.join(x))