【问题标题】:Python/Pandas: Select specific value and return seriesPython/Pandas:选择特定值并返回系列
【发布时间】:2018-05-12 19:20:17
【问题描述】:

我有一个 df,其中包含 20-25、25-30 等形式的“age_bracket”和“no_show”,它的值只有 0 或 1,用于指示患者是否出现在预约或不是。

为了创建条形图,我需要每个年龄显示与未显示的总和比例值。 我试过这个:

noshow_counts = df.groupby('age_bracket')['no_Show'].value_counts()[1]
show_counts = df.groupby('age_bracket')['no_Show'].value_counts()[0]
age_totals = df.groupby('age_bracket').count()['no_Show']

这样计算比例

nowshow_proportions = noshow_counts / age_totals
show_proportions = show_counts /age_totals

这是它在条形图中的使用方式

#Bar Chart
ind = np.arange(len(nowshow_proportions))  
width = 0.40 
# plot bars
noshow_bars = plt.bar(ind, nowshow_proportions, width, color='g', 
alpha=.7, label='No Show')
show_bar = plt.bar(ind + width, show_proportions, width, color='b', 
alpha=.7, label='Show')

这不会产生正确的值。我猜这是因为 value_counts 返回一个对象而不是一个系列。所以这是不正确的

 noshow_counts = df.groupby('age_bracket')['no_Show'].value_counts()[1]
 show_counts = df.groupby('age_bracket')['no_Show'].value_counts()[0]

有没有办法只选择“1”和“0”值并返回一个系列?

【问题讨论】:

  • 您能提供一些示例数据吗?应该如何标准化这些值?跨年龄组或跨显示/未显示?

标签: python pandas matplotlib


【解决方案1】:

我能够得到正确的结果

show_counts = df[df['no_Show'] == 0].groupby('age_bracket').count(). 
['no_Show']
noshow_counts=df[df['no_Show'] == 1].groupby('age_bracket').count(). 
['no_Show']

【讨论】:

    猜你喜欢
    • 2020-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2018-07-28
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多