简体   繁体   English

使用python获取每一行的数组元素的值计数

[英]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.

相关问题 Python从矩阵的每一行中减去第一个元素 - Python subtract first element from each respective row of matrix 在python中,如何通过条件对一维数组的对应元素快速设置二维数组的每一行的值? - In python, how to quickly set the value of each row of a 2 dimensional array by condition on the corresponding element of a one dimensional array? 获取每个元素的反转并将其放在python中的数组中 - getting the inverse of each element and putting it in array in python 从 Python 中的 pandas 列中的每一行获取唯一字数 - Getting the unique word count from each row in a pandas column in Python 为数组python中的每个元素添加值 - add value to each element in array python 计算每行特定值内的numpy数组中的实例 - Count instances in numpy array within a certain value of each row 用各自索引的元组替换多维数组的每个元素 - Replace each element of multidimensional array with tuple of respective indices 如何计算python中每行的值的列数? - How to count the number of columns with a value on each row in python? python 数组计算每个元素 - python array count each elements A pandas dataframe 列作为行级 function 的参数传递,以将列的每个值应用于其各自的行 - A pandas dataframe column to pass as an argument of row level function to apply each value of the column to its respective row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM