Row counts groupby multiple columns for NUMPY array 多列分组的行计数,用于 NUMPY 数组

huangapple go评论66阅读模式
英文:

Row counts groupby multiple columns for NUMPY array

问题

Sure, here is the translated code portion:

import numpy as np

arr = [['a', '1', '2.3'],
       ['a', '1', '1.1'],
       ['b', '2', '3.4'],
       ['a', '2', '10.1']]

# Your code to get row counts groupby the first and the second column [0,1]

Please note that the code to achieve the desired result is not provided in your request, so you will need to implement it yourself or ask for further assistance with that specific part.

英文:

I have a numpy array as the following:

arr =[['a', '1' ,'2.3'],
['a', '1' ,'1.1'],
['b', '2', '3.4'],
['a', '2', '10.1']]

I would like to get row counts groupby the first and the second column [0,1].

I am expecting the following:

arr_counts = [['a', '1', 2],
['b', '2', 1],
['a', '2', 1]]

I known this can be done easily using PANDAS. But I would like to stay in NUMPY array. #So, no PANDAS please. Thank you.

答案1

得分: 0

使用 np.uniquenp.column_stack 的组合:

arr_counts = np.column_stack(np.unique(arr[:, :2], axis=0, return_counts=True))

结果如下:

array([['a', '1', '2'],
       ['a', '2', '1'],
       ['b', '2', '1']], dtype='<U21')

请注意,代码部分不包括在翻译中。

英文:

With np.unique + np.column_stack combination:

arr_counts = np.column_stack(np.unique(arr[:, :2], axis=0, return_counts=True))

array([[&#39;a&#39;, &#39;1&#39;, &#39;2&#39;],
       [&#39;a&#39;, &#39;2&#39;, &#39;1&#39;],
       [&#39;b&#39;, &#39;2&#39;, &#39;1&#39;]], dtype=&#39;&lt;U21&#39;)

huangapple
  • 本文由 发表于 2023年5月18日 00:15:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定