[英]getting value count of a an array element respective to each row with python
input data this is my input data from each array i need its respective count ,if it is not present then 0 will be输入数据这是我来自每个数组的输入数据,我需要其各自的计数,如果不存在,则为 0
ID array
0 20 [1, 2, 1, 1, 2, 3, 4, 5]
1 34 [1, 2, 2, 3, 4, 6, 7]
2 55 [6, 8, 9, 2, 1, 6]
3 66 [3, 4, 7, 7, 8, 1]
output输出
ID array 1 2 3 4 5 6 7 8 9
0 20 [1, 2, 1, 1, 2, 3, 4, 5] 3 2 1 1 1 0 0 0 0
1 34 [1, 2, 2, 3, 4, 6, 7] 1 2 1 1 0 1 1 0 0
2 55 [6, 8, 9, 2, 1, 6] 1 1 1 0 0 0 1 0 1 1
3 66 [3, 4, 7, 7, 8, 1] 1 0 0 1 1 0 0 2 1 0
You can use this line of code for each row of you dataset :您可以对数据集的每一行使用这行代码:
[name_list.count(element) for element in range(1,10)]
where list_name is the list you want to pass as argument and count the recurrences其中 list_name 是您要作为参数传递并计算重复次数的列表
IIUC, here's one way via pandas
: IIUC,这是通过pandas
的一种方式:
df1 = df.explode('array')
df = pd.crosstab(df1['ID'], df1['array'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.