1

I have a dataframe with the following fields. For each Id, I have two records, that represent different latitude and longitudes. I'm trying to achieve a resultant dataframe that groups by current dataframe based on id and put its latitude and longitude into different fields.

I tried with the group by function but I do not get the intended results. Any help would be greatly appreciated.

Id  StartTime   StopTime    Latitude    Longitude
101 14:42:28    14:47:56    53.51       118.12
101 22:10:01    22:12:49    33.32       333.11

Result:

Id  StartLat    StartLong   DestLat DestLong
101 53.51       118.12      33.32       333.11

1 Answer 1

1

You can use groupby with apply function for return flatten DataFrame to Series:

df = df.groupby('Id')['Latitude','Longitude'].apply(lambda x: pd.Series(x.values.ravel()))
df.columns = ['StartLat', 'StartLong', 'DestLat', 'DestLong']
df = df.reset_index()
print (df)
    Id  StartLat  StartLong  DestLat  DestLong
0  101     53.51     118.12    33.32    333.11

If problem:

TypeError: Series.name must be a hashable type

try change Series to DataFrame, but then need unstack with droplevel:

df = df.groupby('Id')['Latitude','Longitude']
       .apply(lambda x: pd.DataFrame(x.values.ravel()))
       .unstack()
df.columns = df.columns.droplevel(0)
df.columns = ['StartLat', 'StartLong', 'DestLat', 'DestLong']
df = df.reset_index()
print (df)
    Id  StartLat  StartLong  DestLat  DestLong
0  101     53.51     118.12    33.32    333.11
Sign up to request clarification or add additional context in comments.

2 Comments

My dataframe has 400000 records. This group by works perfectly for 100 records. For more than 100 records itself, I get the error "TypeError: Series.name must be a hashable type". Not sure why I get this error with increased number of records in the dataframe? Are these restricted by any specific size?
I have this problem also and solution is, give me a minute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.