python - 一个字符串数组到句子数据框 pandas

标签 python arrays pandas dataframe

我的数据框看起来像

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))]

我应该做什么

最佳答案

几乎正确——你有一个额外的层 df[。试试这个:

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

您已经返回了要用于替换 Phrase 的列,但随后您试图按其值进行索引,这会产生错误。

关于python - 一个字符串数组到句子数据框 pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42211208/

相关文章:

python - 如何从 float 中删除字符?

python - pandas 应用并分配给多列

python - 带有中间页面 : not getting info back 的 Django 管理操作

c++ - Char 数组指针返回不正确(当前年份 ctime)

python - 如何绘制 y=x+i for i = [1 :16]?

python - 尝试使用 Deque 来限制传入数据的 DataFrame ......建议?

Python 2to3 脚本不工作 - Unicode 错误

python - 是否可以在 Django Behave 测试中进行模拟?

c++ - 在 C++ 中使用数组获取段错误

python - 将 pandas 中的行拆分为多行