首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重新排列列的值

重新排列列的值
EN

Stack Overflow用户
提问于 2021-11-11 15:02:55
回答 1查看 38关注 0票数 0

我有一个DataFrame,在这里我希望重新排列给定列的数据。

我拥有的:

代码语言:javascript
复制
    text                                                KEYWORD
0   Fetch.ai will transform economies, healthcare,...   supplies chain issues
1                                                       self
2                                                       secured key partnership
3                                                       real world challenge
4                                                       autonomous economic agent
5                                                       learning traffic signal
6                                                       autonomous machine learning
7                                                       disruptive ai tech
8                                                       parking issues
9                                                       traffic reduction
10      
11      
12  The two most popular cryptocurrencies on the p...   bitcoin
13                                                      limited supplies
14                                                      ethereum

我想要的:

代码语言:javascript
复制
    text                                                KEYWORD
0   Fetch.ai will transform economies, healthcare,...   supplies chain issues, self, secured key partnership,  real world challenge, autonomous economic agent, learning traffic signal, autonomous machine learning, disruptive ai tech, parking issues, traffic reduction
1   The two most popular cryptocurrencies on the p...   bitcoin, limited supplies, emphasized text, ethereum

包含文本的每一行都显示在" text“列中。对“文本”列进行了分析,并从中提取了关键字,并将其显示在“关键字”列中。恼人的部分是,如果从"Text“列中提取出10个关键字,它将创建10行,并在每行添加1个关键字。我想将所有这些关键字合并到一行中(与好的文本相对应)。

不幸的是,我无法访问由软件完成的关键字提取过程。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-11 15:09:47

尝试使用groupby

代码语言:javascript
复制
#replace blank cells with NaN
df = df.replace(r"^\s*$",np.nan,regex=True)

#drop rows that are all NaN and forward fill
df = df.dropna(how="all").ffill()

#groupby and aggregate
output = df.groupby("text", as_index=False)["KEYWORD"].agg(", ".join)

>>> output
                                                text                                            KEYWORD
0  Fetch.ai will transform economies, healthcare,...  supplies chain issues, self, secured key partn...
1  The two most popular cryptocurrencies on the p...                bitcoin, limited supplies, ethereum
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69930509

复制
相关文章

相似问题

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