Maxl Gemeinderat
Maxl Gemeinderat

Reputation: 555

How to plot dates on a monthly/weekly basis in pandas dataframe

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:

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!

Upvotes: 2

Views: 7447

Answers (2)

tdy
tdy

Reputation: 41327

only plot months or weeks

Use DataFrame.resample or Series.resample to resample the frequency to M (months) or W (weeks). Just make sure the index is a true datetime type (use pd.to_datetime if needed).

Before resampling:

rng = np.random.default_rng(0)
df = pd.DataFrame({'Date': pd.date_range('2021-01-01', '2021-12-31'), 'Partei': rng.choice(list('abc'), size=365), 'sentiment_prediction': rng.integers(5, size=365)})
df = df.set_index('Date')

df['sentiment_prediction'].plot()

After resampling:

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

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


grouped by "Partei"

Use DataFrame.pivot_table to pivot Partei into columns before plotting:

(df.pivot_table(index='Date', columns='Partei', values='sentiment_prediction')
   .resample('M').mean().plot())

Upvotes: 3

ENCS16
ENCS16

Reputation: 1

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.

Upvotes: 0

Related Questions