首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何转换要列出的列的多个值

如何转换要列出的列的多个值
EN

Stack Overflow用户
提问于 2022-02-15 21:05:44
回答 1查看 938关注 0票数 0

我有一只熊猫DataFrame,有三栏。

代码语言:javascript
复制
df = pd.read_csv('doc.csv')


document    year    Keywords           
     1     2018     Appliance Interaction; Intrusive Load Monitoring; Appliance Identification            
     2     2018      wireless networks; Learning algorithms; operator recommendations           
     3     2019      Natural Language; Crowdsourcing; Natural Language; Sensemaking  

数据:

代码语言:javascript
复制
{'document': [1, 2, 3],
 'year': [2018, 2018, 2019],
 'Keywords': ['Appliance Interaction; Intrusive Load Monitoring; Appliance Identification',
  'wireless networks; Learning algorithms; operator recommendations',
  'Natural Language; Crowdsourcing; Natural Language; Sensemaking']}

我想要做的是将列(关键字)转换为如下所示的列表

代码语言:javascript
复制
X = [Appliance Interaction, Intrusive Load Monitoring, Appliance Identification, wireless networks, Learning algorithms, operator recommendations, Natural Language, Crowdsourcing, Natural Language, Sensemaking]

并将此列表保存到单独的CSV文件中。

EN

回答 1

Stack Overflow用户

发布于 2022-02-15 21:15:29

您可以在"; " + explode上拆分并转换为list:

代码语言:javascript
复制
X = df['Keywords'].str.split('; ').explode().tolist()

输出:

代码语言:javascript
复制
['Appliance Interaction',
 'Intrusive Load Monitoring',
 'Appliance Identification',
 'wireless networks',
 'Learning algorithms',
 'operator recommendations',
 'Natural Language',
 'Crowdsourcing',
 'Natural Language',
 'Sensemaking']
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71133405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档