TheDaJon
TheDaJon

Reputation: 555

python plotting a dataframe

I have a pandas dataframe in Python that looks like this:

  Jan 15   Feb 15   March 15   April 15
1   val      val      val       val
2   val      val      val       val 
3   val      val      val       nan
4   val      nan      val       nan
5   val      nan      nan       nan

I want to plot a graph as follows. The X axis is the months (Jan 15 | Feb 15 | March 15 | April 15), and the bars are like so:

Jan 15: 5
Feb 15: 3
March 15: 4
April 15: 2

^ this is the count of non-null values for each month.

Any help on how to plot it with matplotlib or anything else?

EDIT: the values in the df are tuples like so: (val1, val2).

Upvotes: 1

Views: 640

Answers (2)

IanS
IanS

Reputation: 16241

This will return a series with the data you want to plot:

df.count()

This will plot it:

df.count().plot.bar();  # the semicolon will suppress output in Jupyter

Edit: made it a bar chart.

Upvotes: 2

jezrael
jezrael

Reputation: 862451

You need count with Series.plot.bar:

import matplotlib.pyplot as plt
df.count().plot.bar()
plt.show()

Solution with Series.plot:

import matplotlib.pyplot as plt
df.count().plot(kind='bar')
plt.show()

graph

Upvotes: 2

Related Questions