Home > Software engineering >  Python Groupby and plotting of data
Python Groupby and plotting of data

Time:12-22

Hi I have this dataframe as shown:

enter image description here

Is there a way to plot this ? For eg for 1-5-2021, to add up 10 7=17 for total . The graph need to show the daily statistics i.e.1-5-2021, the total is 17 and temperature is 36c.

sorry i forgot to add on..i want to show if there is a correlation btn temperature and total Thanks!

CodePudding user response:

Try:

#convert date column to datetime
df["Date"] = pd.to_datetime(df["Date"], format="%d-%m-%Y")

#extract numbers from temperature column
df["Temperature"] = df["Temperature"].str.extract("(\d )").astype(float)

#groupby to create the required plot data
df_plot = df.groupby("Date").agg({"Total": "sum", "Temperature": "first"})
df_plot.index = df_plot.index.date

#plot
>>> df_plot.plot.bar()

enter image description here

CodePudding user response:

Do you want a bar plot? then

Edit: thanks to not_speshal for pointing out temperature column

(df.assign(temp=lambda x: x["Temperature"].str.extract("(\d )").astype(float))
.groupby("Date").agg({"Total":"sum","temp":"first"})[["Total","temp"]].plot(kind="bar"))

CodePudding user response:

Considering you are using pandas, the following can help you

pdf.groupby(['Date']).sum()[['Total']]

where pdf is the pandas dataframe containing the data. Then plot this into a graph.

  • Related