python-3.x 计算panda中每行列中特定值个数

g2ieeal7  于 2022-12-05  发布在  Python
关注(0)|答案(2)|浏览(177)

您好,我有一个 Dataframe ,例如:

Species COL1 COL2 COL3 COL4 COL5
SP1     0    0    0    1-2  0-1-2
SP2     1-2  2    0    1    0
SP3     0-1  1    2    0    1-2

并且我希望添加新列,以便为每一行计算特定唯一值的数量,例如:

Species COL1 COL2 COL3 COL4 COL5  count_0 count_1-2 count_0-1-2 count_1 count_2 
SP1     0    0    0    1-2  0-1-2 3       1         1           0       0
SP2     1-2  2    0    1    0     2       1         0           1       1
SP3     0-1  1    2    0    1-2   1       1         0           2       1

有人有什么好主意吗?

bfnvny8b

bfnvny8b1#

您可以在pandas library中使用value_counts()方法来计算每个唯一值在 Dataframe 的每行中出现的次数。

# Loop through each row of the dataframe
for index, row in df.iterrows():
    # Create a series object for the current row
    series = pd.Series(row)

    # Count the number of occurrences of each unique value in the row
    counts = series.value_counts()

    # Add the count values to the current row of the dataframe
    df.loc[index, 'count_0'] = counts[0] if 0 in counts else 0
    df.loc[index, 'count_1-2'] = counts['1-2'] if '1-2' in counts else 0
    df.loc[index, 'count_0-1-2'] = counts['0-1-2'] if '0-1-2' in counts else 0
    df.loc[index, 'count_1'] = counts[1] if 1 in counts else 0
    df.loc[index, 'count_2'] = counts[2] if 2 in counts else 0
piok6c0g

piok6c0g2#

示例

data = {'Species': {0: 'SP1', 1: 'SP2', 2: 'SP3'},
        'COL1': {0: '0', 1: '1-2', 2: '0-1'},
        'COL2': {0: '0', 1: '2', 2: '1'},
        'COL3': {0: '0', 1: '0', 2: '2'},
        'COL4': {0: '1-2', 1: '1', 2: '0'},
        'COL5': {0: '0-1-2', 1: '0', 2: '1-2'}}
df = pd.DataFrame(data)

代码

df1 = (df.set_index('Species').apply(lambda x: x.value_counts(), axis=1)
       .add_prefix('count_').fillna(0).astype('int'))

df1

count_0    count_0-1   count_0-1-2 count_1 count_1-2   count_2
Species                     
SP1      3          0            1          0       1           0
SP2      2          0            0          1       1           1
SP3      1          1            0          1       1           1

生成所需输出

连接df和df1

pd.concat([df.set_index('Species'), df1], axis=1)

相关问题