pandas 计算df中“1”的个数

niwlg2el  于 2023-03-06  发布在  其他
关注(0)|答案(3)|浏览(394)

我有下面的df,如图中的矩阵所示,我想计数等于1的"相关"平方和等于0的"不相关"平方的数量。
我试过使用df. count()函数,但是它没有返回我想要的结果,比如1和0的总和。
任何帮助都很好,谢谢。
我的代码:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

df = pd.read_csv('Res_Gov.csv')  

df1 = df.set_index('Indicators').T 

# Set up the matplotlib figure
fig, ax = plt.subplots(figsize=(12, 12))

colors = ["#f4a261","#2a9d8f"] 
cmap = LinearSegmentedColormap.from_list('Custom', colors, len(colors))

# Draw the heatmap with the mask and correct aspect ratio
df1 = sns.heatmap(df1, cmap=cmap, square=True,
                  linewidths=.5, cbar_kws={"shrink": .5})  # HERE

# Set the colorbar labels
ax.set_xlabel("Indicators")
ax.set_ylabel("Resilience Criteria")
ax.tick_params(axis='x', rotation=90)
colorbar = ax.collections[0].colorbar
colorbar.set_ticks([0.25,0.75])
colorbar.set_ticklabels(['Not Correlated', 'Correlated'])
fig.tight_layout()  
plt.show()

DF片段

,Indicators,Robustness,Flexibility,Resourcefulness,Redundancy,Diversity,Independence,Foresight Capacity,Coordination Capacitiy,Collaboration Capacity,Connectivity & Interdependence,Agility,Adaptability,Self-Organization,Creativity & Innovation,Efficiency,Equity
0,G1,1,1,1,0,0,1,1,1,1,1,1,1,0,1,1,1
1,G2,1,0,0,0,0,1,0,1,1,1,1,0,0,1,1,1
2,G3,1,0,1,0,0,1,1,1,1,1,1,1,1,1,0,1
3,G4,1,1,1,0,1,0,1,1,1,1,1,1,1,1,0,1
4,G5,1,0,1,0,0,0,0,1,0,1,1,0,0,0,1,0
5,G6,1,0,1,0,1,0,1,0,0,0,0,0,0,1,0,1
6,G7,1,1,0,1,0,1,0,0,0,0,1,0,0,0,0,0
7,G8,1,1,0,0,0,1,1,1,1,0,1,1,0,0,0,0
8,G9,1,0,1,0,0,1,1,1,1,0,1,1,0,1,0,1
9,G10,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1
10,G11,1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1
11,G12,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0
12,G13,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0
13,G14,1,0,1,0,1,0,1,1,1,1,1,1,0,0,1,1
14,G15,1,1,1,0,1,0,1,1,1,1,1,1,1,1,0,1
15,G16,1,0,1,0,1,1,0,1,1,1,0,1,1,1,0,1
16,G17,1,1,1,0,0,0,0,0,0,0,1,1,0,1,1,0
17,G18,1,0,1,0,1,1,1,1,1,1,0,1,1,1,0,1
18,G19,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
19,G20,1,1,0,1,1,0,0,0,1,0,0,0,1,0,0,1
20,G21,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1
21,G22,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1
22,G23,1,0,1,0,0,1,1,0,1,0,0,1,1,1,0,0

基质:

vsnjm48y

vsnjm48y1#

您可以将形状调整为Series和value_counts

df.melt('Indicators')['value'].value_counts()

或者:

df.drop(columns='Indicators').stack().value_counts()

输出:

kxeu7u2r

kxeu7u2r2#

您可以检查 Dataframe 中的1,如下所示:
df==1
这返回具有与初始 Dataframe 相同大小的布尔值(True,False)的 Dataframe ,指示每个单元格是否满足条件。
将此逻辑语句的结果与.values.sum()链接将返回 Dataframe 中1的数量。

(df==1).values.sum()

.values命令将带有布尔值的 Dataframe 作为numpy数组返回,.sum()对整个数组求和,将False计为0,True计为1。

mqkwyuun

mqkwyuun3#

您可以简单地使用df1

>>> df1.stack().value_counts()
1    208
0    160
dtype: int64

您也可以使用numpy

>>> dict(zip(*np.unique(df1, return_counts=True)))
{0: 160, 1: 208}

>>> pd.Series(dict(zip(*np.unique(df1, return_counts=True))))
0    160
1    208
dtype: int64

相关问题