1

Hi I have data like below.

sample code

I need to write a code dynamically to input my year and make a plot between month and number of messages. The year 2016 in my data has only 6 months and rest doesn't exist.

I tried setting year as index and tried plotting.

dff[dff.index == 2015].plot(marker='*')

But the plot I did is not what is required.

1 Answer 1

1

Use boolean indexing or query for filtering and then plot with parameters x and y:

year = 2014

df[df['year'] == year].plot(x='month', y='Number of messages', marker='*')

Or:

df.query("year == @year").plot(x='month', y='Number of messages', marker='*')

Another approach is plot Series with index by Month column:

df = df.set_index('month')
df.loc[df['year'] == year, 'Number of messages'].plot(marker='*')

df.set_index('month').query("year == @year")['Number of messages'].plot(marker='*')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.