1

I have used following aggregation dictionary:

fare_agg = {
    'Fare_amount': {
        'mean_fare_amount':'mean',
        'meadian_fare_amount': 'median'
    },
    'Total_amount':{
        'mean_total_amount':'mean',
        'median_total_amount':'median'
    },
    'Trip_distance':{
        'mean_trip_distance':'mean',
        'median_trip_distance':'median'
    }
}

df_g = df_a.groupby('type').agg(fare_agg)

When I save the data to the csv it comes up with two level headers. I tried to remove df_a.columns = df_a.columns.droplevel(0) but that didn't work. Then tried to reset_index(inplace=True) but that also didn't work. Whats the trick to get only the second level header.

Here is how the csv header looks like:

type,Trip_distance,Trip_distance,Fare_amount,Fare_amount,Total_amount,Total_amount
,mean_trip_distance,median_trip_distance,mean_fare_amount,meadian_fare_amount,median_total_amount,mean_total_amount
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
  • 1
    Possible duplicate of [Python Pandas - How to flatten a hierarchical index in columns](https://stackoverflow.com/questions/14507794/python-pandas-how-to-flatten-a-hierarchical-index-in-columns). If this doesn't help, I strongly suggest you provide a [mcve]. – jpp Nov 20 '18 at 17:52
  • Related? https://stackoverflow.com/questions/22233488/pandas-drop-a-level-from-a-multi-level-column-index – Evan Nov 20 '18 at 19:32

0 Answers0