如何计算带条件的行

2024-05-23 17:36:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如何计算每个类别中与标签相比的数量

      Credit  Term      Y
0  Excellent     3   safe
1       fair     5  risky
2       poor     3  risky
3       fair     5  risky
4  Excellent     5   safe
5       poor     3  risky
6  Excellent     5   safe
7       poor     3  risky
8       fair     3   safe
9       fair     5   safe

这是我的数据,我想计算有多少安全的优秀的差和公平的,还有多少风险的优秀的差和公平的,比如优秀的3安全的0风险的等等 我计算:

data[(data['Credit']=='Excellent')&(data['Y']=='safe')].count()
data[(data['Credit']=='Excellent')&(data['Y']=='risky')].count()

elements,counts = np.unique(data['Credit'],return_counts = True)

我怎么能做一张这样的桌子

               safe    risky
excellent      3        0
poor           0        3
fair           2        2

Tags: data数量公平faircount标签类别safe
2条回答

您可以使用pivot_表:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.pivot_table.html

In [16]: df
Out[16]:
      credit  term      Y
0  Excellent     3   safe
1       fair     5  risky
2       poor     3  risky
3  Excellent     5   safe
4       fair     5   safe

In [17]: pd.pivot_table(df, index=['credit'], columns=['Y'], aggfunc='count')
Out[17]:
           term
Y         risky safe
credit
Excellent   NaN  2.0
fair        1.0  1.0
poor        1.0  NaN

In [18]: pd.pivot_table(df, index=['credit'], columns=['Y'], aggfunc='count', fill_value=0)
Out[18]:
           term
Y         risky safe
credit
Excellent     0    2
fair          1    1
poor          1    0

使用熊猫groupby()unstack()

data.groupby(['Credit', 'Y']).count().unstack()

相关问题 更多 >