I have a dataframe with records characterizing roof surfaces of buildings, so each building has multiple planes, with an area and a description of its form. e.g.
df=pd.DataFrame([[1000, 12, 'slope'],
[1000, 10, 'flat'],
[1001, 10, 'slope'],
[1001, 15, 'flat'],
[1001, 7, 'slope']],
index = [1,2,3,4,5],
columns=['building_id', 'area', 'form'],
)
df
building_id area form
1 1000 12 slope
2 1000 10 flat
3 1001 10 slope
4 1001 15 flat
5 1001 7 slope
I want to combine the rows so i have one for each building, with the total roof area and the predominant roof form - ie the form that has the greatest area for that building, not the form that appears most frequently:
df_out
building_id area form
1 1000 22 slope
2 1001 32 slope
I need something like this:
group_functions={'area' : ['sum'],
'form' : lambda x: find_predominant(x)}
df_out = df.groupby('building_id').agg(group_functions)
But find_predominant needs to be a function of area as well as form: It returns the string 'flat' or 'slope' depending on which has the biggest area for that building_id.
What is the function find_predominant ? Or what script will have the same effect?