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

Combining rows of a dataframe base on the values of a column

enter image description here

How can I combine the Percentage Total Production that are less than 20% into 1 row and label that as ‘Others’

so i should only get ‘Other’: 78.43%, India: 21.561239%
The results should be in a dataframe in this format

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

>Solution :

You can concatenate a row that is the sum of all rows where the percentage is < 20, and all the remaining rows:

mask = df['Percentage_Total_Production'] < 20
new_row = pd.DataFrame([df[mask].sum(axis=0)], index=['other'])

pd.concat([new_row, df[~mask]], axis=0)
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