ValueError: Could not interpret input 'State/UnionTerritory'

Viewed 900

My data set containg following data. State/UnionTerritory column containg repeted names of states

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 14690 entries, 0 to 14689
Data columns (total 8 columns):
 #   Column                    Non-Null Count  Dtype         
---  ------                    --------------  -----         
 0   Date                      14690 non-null  datetime64[ns]
 1   State/UnionTerritory      14690 non-null  object        
 2   ConfirmedIndianNational   14690 non-null  object        
 3   ConfirmedForeignNational  14690 non-null  object        
 4   Cured                     14690 non-null  int64         
 5   Deaths                    14690 non-null  int64         
 6   Confirmed                 14690 non-null  int64         
 7   Total_active              14690 non-null  int64         
dtypes: datetime64[ns](1), int64(4), object(3)
memory usage: 918.2+ KB

I'm writing code for bar plot for that I need to group all data by 'State/UnionTerritory' after that I want to plot the bar

data = dataframe[['State/UnionTerritory', 'Confirmed', 'Cured', 'Deaths']]
data.sort_values('Confirmed', ascending = False, inplace = True)
data = data.groupby('State/UnionTerritory')['Confirmed', 'Cured', 'Deaths'].sum().sort_values(by = 
                    'Confirmed', ascending = False)
data

sns.barplot(x = 'Confirmed', y ='State/UnionTerritory', data = data, label = 'Total', color = "r")
sns.set_color_codes("muted")
sns.barplot(x = 'Cured', y = 'State/UnionTerritory', data = data, label = 'Cured', color = "g")

# Add a legend & informative axis lable
ax.legend(ncol = 2, loc = 'lower right', bbox_to_anchor=(1, -0.15), frameon = True)
ax.set(xlim = (0, 35), ylabel = '', xlabel = 'Cases')
sns.despine(left = True, bottom = True)

I did grouping by state names. This is the output of data

State/UnionTerritory   Confirmed    Cured   Deaths
--------------------------------------------------      
Maharashtra            563699160    490137457   13199193
Karnataka              232492536    209887486   3047652
Andhra Pradesh         222339098    209691093   1804369
Tamil Nadu             215959445    202000492   3181512
Kerala                 190487632    170348486   737000

When I try barplot on that data I get this error

4 data
5 
----> 6 sns.barplot(x = 'Confirmed', y ='State/UnionTerritory', data = data, label = 'Total', color = 
        "r")
7 sns.set_color_codes("muted")
8 sns.barplot(x = 'Cured', y = 'State/UnionTerritory', data = data, label = 'Cured', color = "g")

ValueError: Could not interpret input 'State/UnionTerritory'

Anyone know how to solve this. Thanks is advance

1 Answers

What has likely happened is that you no longer have a column called 'State/UnionTerritory'. Instead it is now your index. So you could either reset your index / rename columns or just pass the data directly to X and Y instead of passing column names and "data" separately as you did.

By the way, I think you might want to swap your x and y as well.

Practically speaking have a look at data.columns and data.reset_index().columns.

Related