【问题标题】:Visualizing specific information out of two variables in a data从数据中的两个变量中可视化特定信息
【发布时间】:2021-04-08 15:48:57
【问题描述】:

假设我有这个名为 sample.csv 的 csv 文件:

CODE     AGEGROUP      SEX     CITY      HEALTHSTATUS 
----     ---------     ---     ----      ------------
E101      25 to 29      M      Denver    Recovered
E102      25 to 29      F      Chicago   Recovered
E105      45 to 49      M      Denver    Mild

我想可视化(以条形图显示)根据性别“康复”的人数(因此对于上面的示例,一名男性和一名女性康复)。到目前为止,我的代码仅适用于根据性别和健康状况可视化人数。

以下是计算性发生次数的代码要点:

import pandas as pd
import matplotlib.pyplot as plt

fs = 6
plt.style.use("bmh")
fig = plt.figure()

ax0 = plt.subplot(1, 1, 1)
df["SEX"].value_counts().plot(kind="bar", ax=ax0)
ax0.set_xlabel("Sex", fontsize=fs)
ax0.set_ylabel("Number of People", fontsize=fs)
ax0.tick_params(axis='both', labelsize=fs)
ax0.tick_params(axis='x', labelrotation=00)
ax0.set_title("NUMBER OF POSITIVE CASES BY SEX", fontsize=fs)
plt.tight_layout()
plt.show()

我应该如何让程序根据性别可视化“恢复”的数量?

【问题讨论】:

    标签: python pandas csv matplotlib visualization


    【解决方案1】:

    只是改变:

    df["SEX"].value_counts().plot(kind="bar", ax=ax0)
    

    收件人:

    df["SEX"][df['HEALTHSTATUS'] == 'Recovered'].value_counts().plot(kind="bar", ax=ax0)
    

    完整代码:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    fs = 6
    plt.style.use("bmh")
    fig = plt.figure()
    
    ax0 = plt.subplot(1, 1, 1)
    df["SEX"][df['HEALTHSTATUS'] == 'Recovered'].value_counts().plot(kind="bar", ax=ax0)
    ax0.set_xlabel("Sex", fontsize=fs)
    ax0.set_ylabel("Number of People", fontsize=fs)
    ax0.tick_params(axis='both', labelsize=fs)
    ax0.tick_params(axis='x', labelrotation=00)
    ax0.set_title("NUMBER OF POSITIVE CASES BY SEX", fontsize=fs)
    plt.tight_layout()
    

    【讨论】:

      【解决方案2】:

      如果有兴趣,您可以使用数据透视表并在绘图中使用生成的数据框。 一些不同的数据视图:

      df1 = pd.pivot_table(df, values='SEX', index=['HEALTHSTATUS'],
                          columns=['AGEGROUP'], aggfunc=np.count_nonzero, fill_value=0).reset_index()
      df2 = pd.pivot_table(df, values='SEX', index=['HEALTHSTATUS'],
                          columns=['AGEGROUP', 'CITY'], aggfunc=np.count_nonzero, fill_value=0).reset_index()
      df3 = pd.pivot_table(df, values='AGEGROUP', index=['HEALTHSTATUS'],
                          columns='SEX', aggfunc=np.count_nonzero, fill_value=0).reset_index()
      df4 = pd.pivot_table(df, values='AGEGROUP', index=['SEX'],
                          columns='HEALTHSTATUS', aggfunc=np.count_nonzero, fill_value=0).reset_index()
      

      输出:

      In [234]: df1
      Out[234]:
      AGEGROUP HEALTHSTATUS  25to29  45to49
      0                Mild       0       1
      1           Recovered       2       0
      
      In [235]: df2
      Out[235]:
      AGEGROUP HEALTHSTATUS  25to29        45to49
      CITY                  Chicago Denver Denver
      0                Mild       0      0      1
      1           Recovered       1      1      0
      
      In [236]: df3
      Out[236]:
      SEX HEALTHSTATUS  F  M
      0           Mild  0  1
      1      Recovered  1  1
      
      In [237]: df4
      Out[237]:
      HEALTHSTATUS SEX  Mild  Recovered
      0              F     0          1
      1              M     1          1
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        • 2022-12-17
        • 2021-01-30
        • 2022-01-03
        • 1970-01-01
        • 2011-04-21
        • 2022-07-20
        相关资源
        最近更新 更多