如何通过在列表中添加一定数量的行来对序列数据帧进行分组?

2024-06-17 14:50:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我所拥有的:

Series\ u dataframe=数据帧

0 {‘a’:0, ‘b’:’one’, ‘c’:0}
1 {‘a’:1, ‘b’:’two’, ‘c’:1}
2 {‘a’:2, ‘b’:’three’, ‘c’:2}
3 {‘a’:3, ‘b’:’four; ‘c’:3}
4 {‘a’:4, ‘b’:’five’, ‘c’:4}
5 {‘a’:5, ‘b’:’six’, ‘c’:5}
6 {‘a’:6, ‘b’:’seven’, ‘c’:6}
7 {‘a’:7, ‘b’:’eight’, ‘c’:7}
8 {‘a’:8, ‘b’:’nine’, ‘c’:8}
9 {‘a’:9, ‘b’:’ten’, ‘c’:9}
10 {‘a’:10, ‘b’:’eleven’, ‘c’:10}

如果我输入多个分区…例如,如果分区=5:

我想要的是:一个数据帧,其中的值是一个字典列表

数据帧

0 [{‘a’:0, ‘b’:’one’, ‘c’:0}, {‘a’:1, ‘b’:’two’, ‘c’:1}, {‘a’:2, ‘b’:’three’, ‘c’:2}, {‘a’:3, ‘b’:’four; ‘c’:3}, {‘a’:4, ‘b’:’five’, ‘c’:4}]
1 [{‘a’:5, ‘b’:’six’, ‘c’:5}, {‘a’:6, ‘b’:’seven’, ‘c’:6}, {‘a’:7, ‘b’:’eight’, ‘c’:7}, {‘a’:8, ‘b’:’nine’, ‘c’:8}, {‘a’:9, ‘b’:’ten’, ‘c’:9}]
2 [{‘a’:10, ‘b’:’eleven’, ‘c’:10}]

我尝试的是:

Series_dataframe.groupby(np.arange(Series_dataframe.shape[0]) // partitions)

Tags: 数据dataframeoneseriesthreefour分区five
1条回答
网友
1楼 · 发布于 2024-06-17 14:50:23

使用

In [3752]: s.groupby(s.index // 5).apply(list)
Out[3752]:
0    [{u'a': 0, u'c': 0, u'b': u'one'}, {u'a': 1, u...
1    [{u'a': 5, u'c': 5, u'b': u'six'}, {u'a': 6, u...
2              [{u'a': 10, u'c': 10, u'b': u'eleven'}]
Name: s, dtype: object

详细信息

In [3753]: s
Out[3753]:
0          {u'a': 0, u'c': 0, u'b': u'one'}
1          {u'a': 1, u'c': 1, u'b': u'two'}
2        {u'a': 2, u'c': 2, u'b': u'three'}
3         {u'a': 3, u'c': 3, u'b': u'four'}
4         {u'a': 4, u'c': 4, u'b': u'five'}
5          {u'a': 5, u'c': 5, u'b': u'six'}
6        {u'a': 6, u'c': 6, u'b': u'seven'}
7        {u'a': 7, u'c': 7, u'b': u'eight'}
8         {u'a': 8, u'c': 8, u'b': u'nine'}
9          {u'a': 9, u'c': 9, u'b': u'ten'}
10    {u'a': 10, u'c': 10, u'b': u'eleven'}
Name: s, dtype: object

In [3754]: type(s)
Out[3754]: pandas.core.series.Series

相关问题 更多 >