[英]Get value and key lists out of pandas groupBy
I am using pandas to create three arrays that I need for some stats.我正在使用 pandas 创建一些统计数据所需的三个数组。 I need all the fields, the month and the number of finishes and starts in that month.
我需要所有字段、月份以及该月完成和开始的次数。
My dataframe is the following我的数据框如下
month finish started
0 MONTH.Mar 1 0
1 MONTH.Mar 1 0
2 MONTH.Mar 1 0
3 MONTH.Mar 1 0
4 MONTH.Mar 1 0
5 MONTH.Mar 0 1
6 MONTH.Apr 1 0
7 MONTH.Mar 0 1
8 MONTH.Mar 0 1
9 MONTH.Feb 0 1
I do a groupby:我做一个groupby:
df.groupby('month').sum()
and the output is the following:输出如下:
finish started
month
MONTH.Apr 1 0
MONTH.Feb 0 1
MONTH.Mar 5 3
How can I convert the data into three different lists like this:如何将数据转换为三个不同的列表,如下所示:
['MONTH.Apr','MONTH.Feb','MONTH.Mar']
[1,0,5]
[0,1,3]
I tried to do frame.values.tolist()
but the output was the following:我试图做
frame.values.tolist()
但输出如下:
[[1, 0], [0, 1], [5, 3]]
and it was impossible to get the months.并且不可能得到几个月。
IIUC, try reset_index()
and transposing .T
: IIUC,尝试
reset_index()
并转置.T
:
>>> df.groupby('month').sum().reset_index().T.to_numpy()
array([['MONTH.Apr', 'MONTH.Feb', 'MONTH.Mar'],
[1, 0, 5],
[0, 1, 3]], dtype=object)
Or:或者:
>>> df.groupby('month').sum().reset_index().T.values.tolist()
[['MONTH.Apr', 'MONTH.Feb', 'MONTH.Mar'], [1, 0, 5], [0, 1, 3]]
You can use:您可以使用:
month, finish, started = df.groupby('month', as_index=False) \
.sum().to_dict('list').values()
Output:输出:
>>> month
['MONTH.Apr', 'MONTH.Feb', 'MONTH.Mar']
>>> finish
[1, 0, 5]
>>> started
[0, 1, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.