Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group values and ordinally label groups Pandas

I have a column that contains repetitive houses idshouse_id.
I want to group the similar houses ids as family_labeland give them an ordinal label.

My original data looks like this

df_original = pd.DataFrame({‘house_id’:[‘112’, ‘119’, ‘913’, ‘514’,
‘112’, ‘119’, ‘119’]})

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

house_id
112
119
913
514
112
119
119

My target result looks like the below dataframe

df_result = pd.DataFrame({‘house_id’:[‘112’, ‘119’, ‘913’, ‘514’,
‘112’, ‘119’, ‘119’],
‘family_label’:[‘family1’, ‘family2’, ‘family3’, ‘family4’, ‘family1’, ‘family2’, ‘family2’]})

house_id     family_label
112          family1
119          family2
913          family3
514          family4
112          family1
119          family2
119          family2

So far this is what I have achived.

I used this code

df_original['label'] = df_original.groupby(df_original.house_id).grouper.group_info[0]+1

it generates the below output

house_id  label
112        1
119        2
913        3
514        4
112        1
119        2
119        2

I want to know if my approach is correct and I want to add the word ‘family’ before each number.

>Solution :

You can use a list comprehension and precede family string. Such as:

 df_original['label'] = ["family"+str(x) for x in (df_original.groupby(df_original.house_id).grouper.group_info[0]+1)]

Outputting:

  house_id    label
0      112  family1
1      119  family2
2      913  family4
3      514  family3
4      112  family1
5      119  family2
6      119  family2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading