Home > Back-end >  How to plot dates on a monthly/weekly basis in pandas dataframe
How to plot dates on a monthly/weekly basis in pandas dataframe

Time:02-17

I have following dataframe:

               Date     Embedded_text                                  Partei   sentiment_prediction Sentiment
Date                    
2021-03-26  2021-03-26  AfD wirkt.\n\nSchluss mit dem #Gendergaga\nMDR...   AfD        2    Neutral
2021-03-27  2021-03-27  Im Herbst wird gewählt.\nSchluss mit den Recht...   AfD        1    Negative
2021-03-31  2021-03-31  Behördenwillkür\nFlüchtlingsheime durchgedrück...   AfD        1    Negative
2021-04-01  2021-04-01  Aus Raider wird jetzt Twix \n\nLeider kein #Ap...   AfD        2    Neutral
2021-04-01  2021-04-01  Gendern geht („*innen“)\nImpfen geht nicht.\nD...   AfD        1    Negative
... ... ... ... ... ...

The time range of the dataframe goes from 01.01.2021 until 01.01.2022. Now I want to plot different things over time, for example:

  • Plot "sentiment_prediction" over time
  • Plot "sentiment_prediction" over time, grouped by "Partei"
  • Plot above over all months of the year or over specific weeks

I have selected already "Date" as index. When trying to plot over the whole year, I get following output:

df["sentiment_prediction"].plot()

Plot of "sentiment_prediction

As you see, the plot is full of data points, because every single day is considered in this plot. That is why I want to only plot months or weeks in order to get a more insightful plot. At the moment, I don't know how to solve this.

Thanks in advance!

CodePudding user response:

only plot months or weeks

Use

After resampling:

# resampled weekly
df['sentiment_prediction'].resample('W').mean().plot()

# resampled monthly
df['sentiment_prediction'].resample('M').mean().plot()


grouped by "Partei"

Use

CodePudding user response:

You can try to use a different time format (like MM-DD-YYYY) in the X-axis or just obtain the mean or median by every month to plot a single point.

  • Related