英文:
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.unique
和 np.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([['a', '1', '2'],
['a', '2', '1'],
['b', '2', '1']], dtype='<U21')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论