简体   繁体   English

分组数据并可视化缺失值

[英]Group data and visualize missing values

I do have a large medical dataset that I want to group by hospital then plot graph of missing values per hospital.我确实有一个大型医疗数据集,我想按医院分组,然后绘制每个医院的缺失值图。 Here is how the dataset looks like:以下是数据集的样子:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# intialise data of lists. 
data = {'hospital':['Nick hospital', 'Nick hospital','Nick hospital', 'Krish hospital', 'Krish hospital','Krish hospital'], 
        'NAR_used':[2, 1,np.nan, 2, np.nan,3], 'ipno':[45,np.nan,np.nan,np.nan,65,67]
       } 

# Create DataFrame 
df = pd.DataFrame(data)
df

With this sample dataset, we only have 2 hospitals on hospital variable hence I want to visualize missing values for each hospital.使用这个样本数据集,我们在医院变量上只有 2 家医院,因此我想可视化每家医院的缺失值。 Here is what I tried out这是我尝试过的

grouped = df.groupby(['hospital'])
for (i in grouped):
    null_counts = df.isnull().sum()/len(df)
    plt.figure(figsize=(16,8))
    plt.xticks(np.arange(len(null_counts)) + 0.5, null_counts.index, 
    rotation = 'vertical')
    plt.ylabel('Fraction of rows with missing data')
    plt.bar(np.arange(len(null_counts)), null_counts)

My solution is not generating graphs for each Hospital.我的解决方案不是为每个医院生成图表。 Kindly help.请帮忙。 Expected output is visualizing a bar graph of missing values for each hospital .预期输出是可视化每家医院缺失值的条形图。

Let's try:咱们试试吧:

df.set_index('hospital').isna().sum(level=0).plot.bar()

Output:输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM