Reputation: 1278
I have a dataframe df of gps points. I had geographical region that I divided into grid. Each grid cell is represented by pair of two columns (row, col) in a dataframe. The GPS points are labelled with their transportation modes. I want to calculate probability distribution of each grid cell by its transportation modes. (there are five modes of transportation, i.e. walk, bike, car, train, subway).
Row Col P(Walk) P(Bike) P(Car) P(Train) P(Subway)
8 8 Freq(walk)/n Freq(bike)/n Freq(car)/n Freq(train)/n Freq(subway)/n
8 9 Freq(walk)/n Freq(bike)/n Freq(car)/n Freq(train)/n Freq(subway)/n
8 10 Freq(walk)/n Freq(bike)/n Freq(car)/n Freq(train)/n Freq(subway)/n
For example grid cell at row 8, col 8 contains 638 gps points. 598 walk points and 40 subway points Then probability of each transportation mode for this specific grid cell becomes
Row Col P(Walk) P(Bike) P(Car) P(Train) P(Subway)
8 8 598/638 0/638 0/638 0/638 40/638
8 9 ... ... ... ... ...
8 10 ... ... ... ... ...
... ... ... ... ... ... ...
'''
grp = df.groupby(['row','col','Transportation_Mode'])
One way is to iterate over each group one by one using for loops to get the frequency of each transportation mode. But I think their should be more easier or pandorizable way or library that can solve this in just few lines.
An image of geographical region is attached for better understanding of the problem where each geographical region is divided into grid cells represented by rows and cols. Each grid cell contains multiple gps points labelled with their transportation modes.
The csv file of dataframe is available in given link for more clarity of data. https://drive.google.com/open?id=1R_BBL00G_Dlo-6yrovYJp5zEYLwlMPi9
Upvotes: 1
Views: 67
Reputation: 1604
If I'm not mistaken, you're looking for a more elegant way to loop over each group object and generate a 2-dimensional probability distribution for each one?
It sounds like you should look into this pandas documentation (more specifically the apply
function).
You could simply apply a visualization to each group such as this SNS KDE visualization and then join the individual plots back into a grid like the one you provided. With a little ax
magic, you can construct a grid for each transportation type. I think those are the best tools at hand to use. I'll leave the logic to you.
Upvotes: 1