Skip to main content
edited title
Link

How do I create a Geodataframe which the data is grouped by useridUser ID & the timestamp is sorted from oldest to most recent?

Source Link

How do I create a Geodataframe which the data is grouped by userid & the timestamp is sorted from oldest to most recent?

I've been struggling to complete this exercise (Exercise 2 Problem 3) which you guys can refer to this link. A reminder that the kruger_points.shp is obtained from Exercise 2 Problem 2. Here's the specific problem that I'm trying to tackle.

enter image description here

Here's the head of the original dataframe (kruger_points.shp). enter image description here

I'm trying to create a new geodataframe which the data is grouped according to userid and the timestamp is sorted from oldest to most recent. The code is written down below:

sorted_test = {"userid": [], "timestamp":[]}

for name, group in grouped_by_users:
    iterate_for_userid = group["userid"].sort_values(ascending = True)
    sorted_test["userid"].append(iterate_for_userid)
    iterate_for_timestamp = group["timestamp"].sort_values(ascending = True)
    sorted_test["timestamp"].append(iterate_for_timestamp)

sorted_df = geopandas.GeoDataFrame(sorted_test)
sorted_df.head()  

When I checked the new dataframe, I realised that the row contained everything instead of timestamp and userid only.

enter image description here

Therefore, what is the proper way to solve this problem?