Reputation: 33
I am trying to plot bar graph using pandas. DataTime is index column which I get from timestamp. Here is table structure:
So far i have written this:
import sqlite3
from pylab import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime as dt
conn = sqlite3.connect('DEMO2.sqlite')
df = pd.read_sql("SELECT * FROM Data", conn)
df['DateTime'] = df['DATE'].apply(lambda x: dt.date.fromtimestamp(x))
df1 = df.set_index('DateTime', drop=False)
grouped= df1['DateTime'].groupby(lambda x: x.month)
#df1.groupby([df1.index.month, 'DateTime']).count()
grouped.count()
I want output like this:
June has total 4 entry and one entry starts with u. so X has 4 y has 1. Same for July.
Also i want to plot bar graph (X and Y entries) using output. I want MONTH vs Values bar graph
Upvotes: 1
Views: 1526
Reputation: 375475
I would created the DataFrame with a dict:
result = pd.DataFrame({'X': g.count(),
'Y': g.apply(lambda x: x.str.startswith('u').sum())})
Now you can use the plot method to plot months vs values.
result.plot()
Note: you can create grouped more efficiently:
grouped = df1['DateTime'].groupby(df1['DateTime'].dt.to_period('M'))
grouped = df1['DateTime'].groupby(df1['DateTime'].dt.month) # if you want Jan-2015 == Jan-2014
Upvotes: 1