More pymongo

13 Sep

20130913-134209.jpg

I am back in MongoDB mode. I grabbed some pollen data from the City of Albuquerque. It has a date, location, pollen type and the count from 2004 to 2013. I loaded this in to MongoDB from a CSV with this script:
from pymongo import MongoClient

client=MongoClient()
db=client.abq
c=db.pollen

file=open(“pollen.csv”)
for x in file.readlines():
temp=x.split(“,”)
tempdate=temp[0].split(“T”)
date=tempdate[0]
location=temp[1]
type=temp[2]
count=temp[3]
c.insert({“date”:date,”location”:location,”type”:type,”count”:count})

I can then query for elm data sorted by date.
from pymongo import MongoClient

client=MongoClient()
db=client.abq
c=db.pollen

file=open(“elm.txt”,”w+”)

x=c.find({“type”:”Elm”}).sort(“date”)
for entry in x:
s=entry[“count”]
file.write(s+”\n”)

Great! But I want all elm data on the east side of ABQ sorted by date and plotted. Easy! Matplotlib and Pandas help out here:

from pandas import Series
import matplotlib.pyplot as plt
from pymongo import MongoClient
client=MongoClient()
db=client.abq
c=db.pollen
x=c.find({“$and”:[{“type”:”Elm”},{“location”:”EASTSIDE”}]}).sort(“date”)

b=[]
labels=[]

for w in x:
s=w[“count”]
i=int(s)
b.append(i)
t=w[“date”]
labels.append(t)

a=Series(b,index=labels)
Series.plot(a,kind=’bar’)

plt.show()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: