pandas: 合并行中的文本
从下面这个数据表开始;
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
1 个回答
2
用空格把这些项目连接起来:
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.']