gpt4 book ai didi

python - pandas:组合行中的文本

转载 作者:行者123 更新时间:2023-12-01 05:21:46 26 4
gpt4 key购买 nike

来自以下数据框;

data1 = pd.DataFrame({'Section':[1,1,1,2,2,2,2],'Sub':['What','is','this?','I','am','not','sure.']})

如何获得与此类似的结果;

['What is this?','I am not sure.']

到目前为止,我只能想出这样的groupby

for d in data1.groupby(['Section'])['Sub']:
print d[1]

这会给你这样的东西;

0     What
1 is
2 this?
Name: Sub, dtype: object
3 I
4 am
5 not
6 sure.
Name: Sub, dtype: object

最佳答案

用空格连接项目:

In [34]: for d in data1.groupby(['Section'])['Sub']:
...: print ' '.join(d[1])
What is this?
I am not sure.

并将它们列成一个列表:

In [35]: [' '.join(d[1]) for d in data1.groupby(['Section'])['Sub']]
Out[35]: ['What is this?', 'I am not sure.']

关于python - pandas:组合行中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com