熊猫将列表的列转换为文本数据预处理列
我有一个看起来像这样的数据集:
情感 | 文本 |
---|---|
阳性 | ['chewy',''','dhepburn','sed'] |
中性 | ['chewy','plus','you','ve',' '] |
,我想将其转换为:
中性 | 我基本上想将“文本 |
---|---|
性 | dhepburn所说的 |
咀嚼 | ,您添加了 |
”列(由列表组成的“文本”列转换为文本列。
我已经完成了此代码的多个版本:
def joinr(words):
return ','.join(words)
#df['text'] = df.apply(lambda row: joinr(row['text']), axis=1)
#df['text'] = df['text'].apply(lambda x: ' '.join([x]))
df['text'] = df['text'].apply(joinr)
而且我一直得到类似于此代码的东西:
情感 | 文本 |
---|---|
呈阳性 | ['chew y','wha t','dhepbur n','sai d'] |
中性 | ['chew y', 'plu s','yo u','v e','adde d'] |
这是ML模型预处理的数据。我正在Google Colab(类似于Juypter Notebook)工作。
I have a data set that looks like this:
sentiment | text |
---|---|
positive | ['chewy', 'what', 'dhepburn', 'said'] |
neutral | ['chewy', 'plus', 'you', 've', 'added'] |
and I want to convert it to this:
sentiment | text |
---|---|
positive | chewy what dhepburn said |
neutral | chewy plus you ve added |
I basically want to convert the 'text' column, which is made up of lists, into a column of text.
I've done multiple versions of this code:
def joinr(words):
return ','.join(words)
#df['text'] = df.apply(lambda row: joinr(row['text']), axis=1)
#df['text'] = df['text'].apply(lambda x: ' '.join([x]))
df['text'] = df['text'].apply(joinr)
and I keep getting something that resembles this:
sentiment | text |
---|---|
positive | ['c h e w y', 'w h a t', 'd h e p b u r n', 's a i d'] |
neutral | ['c h e w y', 'p l u s', 'y o u', 'v e', 'a d d e d'] |
This is apart of data pre-processing for a ML model. I'm working in Google Colab (similar to Juypter Notebook).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信您的问题是轴= 1您不需要
I believe your problem is the axis = 1 you don't need that
使用
JOIN
:演示:
输出:
基于评论:
另外,您可以简单地使用:
输出:
Use
join
:Demonstration:
Output:
Based on the comment:
Also, you can use more simply:
Output:
如果您有一个列表的字符串表示形式,则可以使用:
如果真的只想删除括号和逗号,请使用正则:
输出:
If you have a string representation of a list you can use:
If really you just want to remove the brackets and commas, use a regex:
Output: