Home > Back-end >  How can I plot line chat of a row in python?
How can I plot line chat of a row in python?

Time:01-10

How can I plot line chat of a row in python?

I have extracted a row like this:

cases_df[cases_df['Country/Region']=='India']

I have extracted a row like this and want to plot a line chart of it to see rise in covid case.

cases_df[cases_df['Country/Region']=='India'].plot()

This code is not working

How to plot line chart of this row?

CodePudding user response:

I think the easiest way to do it is to select the data columns and transpose them:

df_ = cases_df[cases_df['Country/Region']=='India']

#Select all columns after the 4th
df_ = df_.iloc[:, 4:]

#Transpose
df_ = df_.T

#Optional: convert index to dates for a better-looking plot
df_ = df_.set_index(pd.to_datetime(df_.index, format = "%m/%d/y"))

df_.plot()

CodePudding user response:

I came up with this code:

#Remove unnecessary columns
a = cases_df.drop(columns=["Province/State", "Lat", "Long"])

#Select India
a[a['Country/Region']=='India']

#Remove catarogical columns completely
b = cases_df.drop(columns=["Province/State", "Country/Region", "Lat", "Long"])

#Plotting India cases numerical columns
plt.figure(figsize=(23,6))
b.iloc[a[a['Country/Region']=='India'].index[0]].plot()

The problem was numerical and categorical columns are mixed. So, I separated them first.

  •  Tags:  
  • Related