0

When I make a dataframe with:

freq = pd.DataFrame(combined.groupby(['Latitude', 'Longitude','from_station_name']).agg('count')['trip_id'])

It works just fine, but when I attempt:

freq = pd.DataFrame(combined.groupby(['Latitude', 'Longitude','from_station_name']).agg('count')['trip_id'], columns = ['lat','long','station','trips'])

I just see the headers when I look at the dataframe. I can make the dataframe and then use:

freq.columns = ['lat','long','station','trips']

But was wondering how to do this in one step. I've tried specifying "data =" for the aggregate function. Tried double enclosing the brackets for the column names, removing the brackets for the column names. Any advice is appreciated.

Michael
  • 749
  • 1
  • 8
  • 22
  • combined.groupby(['Latitude', 'Longitude','from_station_name']).agg('count') returns a DataFrame, what's the purpose of passing it to DataFrame constructor? You can simply reset_index and rename columns: combined.groupby(['Latitude', 'Longitude','from_station_name']).agg('count').reset_index().rename({'attitude':'lat'...... – Vaishali Jan 30 '19 at 21:58
  • @Vaishali I'm just trying to get this down to one step. I am seeing the same behavior when I do an aggregate that results in a series as well. Ex: latcounts = pd.DataFrame(combined.groupby('Latitude').agg('count'),columns = ['lat','count']) – Michael Jan 30 '19 at 22:11

1 Answers1

0

You don't need to pass your groupby object into a new dataframe constructor (like @Vaishali mentioned already)

If you want to rename your columns after groupby, you can simply do something like:

combined.groupby(['Latitude', 'Longitude','from_station_name']).trip_id.agg('count').rename(columns={'Latitude': 'lat', 'Longitude': 'long', 'from_station_name':'station', 'count': 'trips})
panktijk
  • 1,574
  • 8
  • 10