[英]Taking mean across rows grouped by a variable in numpy
I have an numpy array like below.我有一个像下面这样的 numpy 数组。
array([[ 0.23810484, 0.00020161, 0.41350806, 0.2421371 , 0.02237903,
0.08084677, 0.00020161, 0.00221774, 0.00020161, 0.00020161],
[ 0.04279661, 0.05974576, 0.02584746, 0.00042373, 0.00042373,
0.00042373, 0.00042373, 0.73771186, 0.00889831, 0.12330508]])
It is 5000X10.它是 5000X10。
I also have a Pandas Series object which is again 5000 length.我还有一个 Pandas Series 对象,它的长度也是 5000。 Its values are like this>它的值是这样的>
5061 Terminated
17410 Completed Negative
There are total three distinct cateogries.总共有三个不同的类别。 Each series value is a category for the corresponding row in the first numpy array.每个系列值是第一个 numpy 数组中相应行的类别。
What I want to get is to take an average of each variable in first array grouped by the categories in Series.我想要得到的是取按系列中类别分组的第一个数组中每个变量的平均值。 So in the end I would have a numpy array with 3 rows for each category of series and ten columns whose value will be average across all 5000 rows.所以最后我会有一个 numpy 数组,每个系列类别有 3 行,10 列的值将是所有 5000 行的平均值。
Please advise请指教
You can add each column from the numpy array to a separate column in the pandas DataFrame, and then use DataFrame.groupby()
to group based on your required column and then take mean()
.您可以将 numpy 数组中的每一列添加到 Pandas DataFrame 中的单独列,然后使用DataFrame.groupby()
根据所需列进行分组,然后采用mean()
。 Example (Assuming your series is called series , and numpy array is called narray
) -示例(假设您的系列称为 series ,而 numpy 数组称为narray
)-
df = pd.DataFrame(series)
for i in range(10):
df[i] = narray[:,i]
df.groupby('required_column').mean()
Demo -演示 -
In [77]: df = pd.DataFrame([[5061,'Terminated'],[17410,'Completed Negative']],columns=['index','groupcol']).set_index('index')
In [78]: df
Out[78]:
groupcol
index
5061 Terminated
17410 Completed Negative
In [79]: x
Out[79]:
array([[ 2.38104840e-01, 2.01610000e-04, 4.13508060e-01,
2.42137100e-01, 2.23790300e-02, 8.08467700e-02,
2.01610000e-04, 2.21774000e-03, 2.01610000e-04,
2.01610000e-04],
[ 4.27966100e-02, 5.97457600e-02, 2.58474600e-02,
4.23730000e-04, 4.23730000e-04, 4.23730000e-04,
4.23730000e-04, 7.37711860e-01, 8.89831000e-03,
1.23305080e-01]])
In [80]: for i in range(10):
....: df[i] = x[:,i]
....:
In [81]: df
Out[81]:
groupcol 0 1 2 3 4 \
index
5061 Terminated 0.238105 0.000202 0.413508 0.242137 0.022379
17410 Completed Negative 0.042797 0.059746 0.025847 0.000424 0.000424
5 6 7 8 9
index
5061 0.080847 0.000202 0.002218 0.000202 0.000202
17410 0.000424 0.000424 0.737712 0.008898 0.123305
In [82]: df.groupby('groupcol').mean()
Out[82]:
0 1 2 3 4 \
groupcol
Completed Negative 0.042797 0.059746 0.025847 0.000424 0.000424
Terminated 0.238105 0.000202 0.413508 0.242137 0.022379
5 6 7 8 9
groupcol
Completed Negative 0.000424 0.000424 0.737712 0.008898 0.123305
Terminated 0.080847 0.000202 0.002218 0.000202 0.000202
If you want the result as a list , you can do -如果你想要一个列表的结果,你可以这样做 -
df.groupby('required_column').mean().values.tolist()
Demo -演示 -
In [83]: df.groupby('groupcol').mean().values.tolist()
Out[83]:
[[0.04279661,
0.05974576,
0.02584746,
0.00042373,
0.00042373,
0.00042373,
0.00042373,
0.73771186,
0.00889831,
0.12330508],
[0.23810484,
0.00020161,
0.41350806,
0.2421371,
0.02237903,
0.08084677,
0.00020161,
0.00221774,
0.00020161,
0.00020161]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.