Reputation: 1088
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. 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 .
Upvotes: 0
Views: 462
Reputation: 150785
Let's try:
df.set_index('hospital').isna().sum(level=0).plot.bar()
Output:
Upvotes: 1