JavaScript
x
28
28
1
import pandas as pd
2
import seaborn as sns
3
regions = ['Eastern Asia','Eastern Asia','Eastern Asia', 'South Asia','South Asia','South Asia']
4
country=['a','b','c','d','e','f']
5
total_vaccine =[12, 15, 16, 18,20,50]
6
7
df = pd.DataFrame(list(zip(regions,country,total_vaccine)),
8
columns=['region','country', 'Vaccine'])
9
df.head()
10
grp=df.groupby('region').apply(lambda x:x.nlargest(2,"Vaccine")).reset_index(drop=True)
11
grp.head()
12
13
df1=grp['region'].unique()
14
df1
15
16
df1=pd.DataFrame(df1)
17
df1
18
19
df1.rename(columns={0:'Regions'},inplace=True)
20
df1
21
22
def plot(region):
23
for region in df1["Regions"]:
24
df_100=grp[grp['region'] == region]
25
if not df_100.empty:
26
sns.barplot( df_100['country'], df_100['Vaccine'], palette ='coolwarm')
27
plot('South Asia')
28
Plotting the graph for both South Asia and Eastern Asia using the above function is showing the same countries and same graphs .What mistake am I doing while writing the above code, I can’t figure that out?
Advertisement
Answer
The problem is with your function. Remove the for loop and it should work
JavaScript
1
5
1
def plot_graph(region):
2
df_100=grp[grp['region'] == region]
3
if not df_100.empty:
4
sns.barplot( df_100['country'], df_100['Vaccine'], palette ='coolwarm')
5