I have a pandas.DataFrame that will not change as I expect. While pivot_table
is ordering everything correctly, the fact that it uses aggregated functions to get a trip. Also, pivot_table
seems to return an unnecessary complex object, rather than a flat data frame.
Consider the following example.
import pandas as pd df = pd.DataFrame({'firstname':['Jon']*3+['Amy']*2, 'lastname':['Cho']*3+['Frond']*2, 'vehicle':['bike', 'car', 'plane','bike','plane'], 'weight':[81.003]*3+[65.6886]*2, 'speed':[29.022, 95.1144, 302.952, 27.101, 344.2],}) df.set_index(['firstname','lastname','weight']) print('------ Unnecessary pivot_table does averaging ------') print(pd.pivot_table(df, values='speed', rows='firstname','lastname','weight'], cols='vehicle')) print('------ pivot method dies ------') print(df.pivot( index=['firstname','lastname','weight'], columns='vehicle', values='speed'))
pivot_table
results:
vehicle bike car plane firstname lastname weight Amy Frond 65.6886 27.101 NaN 344.200 Jon Cho 81.0030 29.022 95.1144 302.952
Is there a way to get pivot
to give essentially the same result as the pivot_table
command (but hopefully flatter and tidier)? Otherwise, how to smooth the output of pivot_table
? What I want as a conclusion is something more:
firstname lastname weight bike car plane Amy Frond 65.6886 27.101 NaN 344.200 Jon Cho 81.0030 29.022 95.1144 302.952