Positive Reflection

Amal fellowship is the most beautiful journey of my life that is going to end within two weeks. I never thought that this fellowship would have a great impact on my life. My facilitators, my circle…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Mapping Divvy Data Using Python

The best part of any project is naming it. My project: Hot Bikes. The goal: make a heat map of where people are biking in Chicago, using the amazing Divvy data set.

After outlining the project with Max, it was determined that the first step was just putting Divvy stations on a map. Alright Max, challenge accepted. Oh yeah, and do it all with PYTHON. Very exciting.

So I Googled “making maps with Python” and lo and behold, someone has done almost exactly what I wanted to do with the bike data available from the bike share program in New York City!

In this article, I’ll be talking about how I created a map, using Python, to show the departures and arrivals at Divvy stations here in Chicago.

So I had my trip data (I would be able to aggregate trip counts from here in order to get departures and arrivals for each station, which I wanted to put on the map).

These data include:

So now I have two different data frames, one with the trip data, which has the id numbers of the start and end stations, but not their latitude and longitude, and I have a data frame with all of the station ids and their latitudes and longitudes.

To get the map that I wanted, these two data sets needed to become one. The magic was going to happen in the following function:

So, what’s happening in this function? Great question. In order to write it, I relied pretty heavily on the aforementioned mapping tutorial, and there were several things that I did not quite understand in it.

After even more digging, I discovered the following:

Calling groupby in pandas returns a groupby object, which has a number of aggregate methods attached to it. Among other things, groupby can take a string which represents a column in a dataframe, and it returns an object which is a grouping of dataframes, each dataframe in the group created from instances of the original dataframe that also contain the specified key. (I think this is accurate).

and:

will both throw errors. Also, if you’re wondering about the double brackets around the zero and why it exists, here is what I know. If you do this:

The object that is returned is not a dataframe, but a series. If you do this:

The object is a dataframe. I do not know why. The rest of the code was written with the necessity of the returned thing being a dataframe, so I needed to keep the brackets. It’s not quite magic, but almost. My mentor Max says, “There’s no such thing as magic.” But we can still have fun, can’t we?

The next line of code:

Changes the dataframe into one that has an index of id and a column name of “Departure Count.”

Then, to get the arrival counts, I do the exact same thing, but instead grouping by the arrival station id and getting a dataframe that has one column “Arrival Count” and an index of station id:

Finally, I merge it all into one big happy family with these few lines of code:

Then, we return the now-merged station data which has our happy latitude and longitude for all of the stations. This is what we wanted.

Now, to put it all on a map!

Folium circle markers are delightful little markers that look like circles and when you hover over them, text is revealed! So, I create one of those for every row too, and then add them to the map, and return the map.

And that’s how I put little circles with arrival and departure counts from each Divvy station on a map of Chicago.

If you made it to the end, wow! WOW. wow. w . o . w . WOWOWOWOWOWOWOWOWOWOWOWOWOWOWOWOWOW.

I ❤ you.

Add a comment

Related posts:

Google Summer of Code 2019 Final Report

This post is a summary of my journey through (Google Summer of Code) GSOC and my interaction with the open-source community of Helikar Lab . I worked on a small project to visualize a way of drawing…

How To Make A Woman Crave The Dick More

I know the thought of being dominant with a woman seems like a jail sentence in today’s society due to 90% of masculinity being “toxic,” but as long as you’re not actually toxic, then there shouldn’t…

Steaksgiving

The first time I cooked a steak for Thanksgiving was in my senior year of college. Before college, my family had the same tradition every year. We would go to my Uncle Bill and Auntie Loretta’s house…