我确实有一个大型的医疗数据集,我想按医院分组,然后绘制每个医院的缺失值图表。下面是数据集的外观:
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
有了这个样本数据集,我们只有2个医院的医院变量,因此我想可视化每个医院的缺失值。这是我尝试过的
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)
我的解决方案不是为每个医院生成图表。请帮帮忙。预期输出是可视化每个医院的缺失值的条形图。
发布于 2020-09-03 05:39:19
让我们试一试:
df.set_index('hospital').isna().sum(level=0).plot.bar()
输出:
https://stackoverflow.com/questions/63713721
复制相似问题