根据单词数将列拆分为未知数量的列 - Pandas [英] Split column into unknown number of columns according to number of words- Pandas
本文介绍了根据单词数将列拆分为未知数量的列 - Pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Pandas 数据框,其中一列包含一些字符串.我想根据字数将该列拆分为未知数量的列.
I have a pandas data frame with one of its column containing some string. I want to split that column into an unknown number of columns according to word count.
假设,我有 DataFrame df
:
Suppose, I have DataFrame df
:
Index Text
0 He codes
1 He codes well in python
2 Python is great language
3 Pandas package is very handy
现在我想将文本列分成多列,每列包含 2 个单词.
Now I want to divide the text column into multiple columns, each containing 2 words each.
Index 0 1 2
0 He codes NaN NaN
1 He codes well in python
2 Python is great language NaN
3 Pandas package is very handy
我怎样才能在 python 中做到这一点?请帮忙.提前致谢.
How can I do this in python? Please help. Thanks in advance.
推荐答案
IIUC,我们可以str.split
groupby
cumcount
with floor Division和 unstack
IIUC, we can str.split
groupby
cumcount
with floor division and unstack
s = (
df["Text"]
.str.split("\s", expand=True)
.stack()
.to_frame("words")
.reset_index(1, drop=True)
)
s["count"] = s.groupby(level=0).cumcount() // 2
final = s.rename_axis("idx").groupby(["idx", "count"])["words"].agg(" ".join).unstack(1)
print(final)
count 0 1 2
idx
0 He codes NaN NaN
1 He codes well in python
2 Python is great language NaN
3 Pandas package is very handy
这篇关于根据单词数将列拆分为未知数量的列 - Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文