将 3 列合并为一列 pandas

Combine 3 columns to one column pandas

我有以下代码:

input= pd.DataFrame({'Police District Name': ['WHEATON', 'SILVER SPRING', 'BETHESDA','GERMANTOWN','WHEATON','MONTGOMERY VILLAGE'], 
                   'cn1': ['Crime Against Person', 'Crime Against Person', 'Crime Against Person','other','other','other'],
                  'cn2': ['Aggravated Assault', 'bla', 'bla','blaa','bla','one more  bla'],
                   'cn3': ['Aggravated Assault', 'bla', 'bla','blaa','bla','one more  bla'],

                    })
input

期望的输出:

output= pd.DataFrame({'Police District Name': ['WHEATON', 'SILVER SPRING', 'BETHESDA','GERMANTOWN','WHEATON','MONTGOMERY VILLAGE'], 
                       'total crime number':[6,3,3,3,6,3],

                    })
output

我怎样才能得到这个?。 谢谢!

如果cn1cn2中的每一个值都填上了犯罪,就可以使用列数。这个想法是通过 value_counts 构造一系列计数并乘以 cnx 列的数量。然后映射到您的数据框。

counts = df['Police District Name'].value_counts() * (len(df.columns) - 1)
df['total crime number'] = df['Police District Name'].map(counts)

print(df[['Police District Name', 'total crime number']])

  Police District Name  total crime number
0              WHEATON                   6
1        SILVER SPRING                   3
2             BETHESDA                   3
3           GERMANTOWN                   3
4              WHEATON                   6
5   MONTGOMERY VILLAGE                   3