Sam Russo
Sam Russo

Reputation: 145

Groupby in Pandas

My code is working, which is good lol, but the output needs to be different in how it is viewed.

UPDATED CODE SINCE RECIEVING ANSWER

import pandas as pd

# Import File
YMM = pd.read_excel('C:/Users/PCTR261010/Desktop/OMIX_YMM_2016.xlsx').groupby(['Make','Model']).agg({'StartYear':'min', 'EndYear':'max'})

print(YMM)

The output looks like Make | Model | StartYear | EndYear, with all the makes listed down column the Make Column next to the Model Column. But the Makes are filtered like a Pivot table.

Here is a screen shot:
enter image description here

I need American Motors next to every American Motors Model, every Buick next to every Buick Model and so on.

Here is the link to sample data: http://jmp.sh/KLZKWVZ

Upvotes: 1

Views: 57

Answers (2)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210812

Try this:

res = YMM.groupby(['Make','Model'], as_index=False).agg({'StartYear':'min', 'EndYear':'max'})

or

res = YMM.groupby(['Make','Model']).agg({'StartYear':'min', 'EndYear':'max'}).reset_index()

Upvotes: 2

BENY
BENY

Reputation: 323226

With your own code

Min = YMM.groupby(['Make','Model']).StartYear.min()
Max = YMM.groupby(['Make','Model']).EndYear.max()

Min['Endyear']=Max.EndYear

Upvotes: 1

Related Questions