如何将数据框的列表行转换为字符串?
how to make data frame's list rows to string?
我有一个数据框,每一行都是这样的列表:
word_count id token
0 6 0 [customer, executive]
1 6 0 [purchase]
2 6 0 [purchase, agent]
3 6 0 [housekeeping]
4 6 0 [night, auditor, &, guest, reception, agent]
我想将列表转换为这样的字符串:
word_count id token
0 6 0 customer executive
1 6 0 purchase
2 6 0 purchase agent
3 6 0 housekeeping
4 6 0 night auditor & guest reception agent
我做了以下方法,但是当我看到 de.head() 没有任何变化时:
for index, row in de.iterrows():
row['token']=' '.join(str(e) for e in row['token'])
de.head()
和
for index, row in de.iterrows():
row['token']=' '.join(row['token'])
de.head()
和
for index, row in de.iterrows():
row['token']=' '.join(row['token']).replace(',' ,' ')
de.head()
列表理解:
df['token'] = [' '.join(x) for x in df['token']]
或apply
:
df['token'] = df['token'].apply(' '.join)
我有一个数据框,每一行都是这样的列表:
word_count id token
0 6 0 [customer, executive]
1 6 0 [purchase]
2 6 0 [purchase, agent]
3 6 0 [housekeeping]
4 6 0 [night, auditor, &, guest, reception, agent]
我想将列表转换为这样的字符串:
word_count id token
0 6 0 customer executive
1 6 0 purchase
2 6 0 purchase agent
3 6 0 housekeeping
4 6 0 night auditor & guest reception agent
我做了以下方法,但是当我看到 de.head() 没有任何变化时:
for index, row in de.iterrows():
row['token']=' '.join(str(e) for e in row['token'])
de.head()
和
for index, row in de.iterrows():
row['token']=' '.join(row['token'])
de.head()
和
for index, row in de.iterrows():
row['token']=' '.join(row['token']).replace(',' ,' ')
de.head()
列表理解:
df['token'] = [' '.join(x) for x in df['token']]
或apply
:
df['token'] = df['token'].apply(' '.join)