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

How to convert comma separated numbers from a dataframe to to numbers and get the avg value

I’m working on a dataset where a column is having numbers separated by commas.
I want to convert the values into integer and obtain their mean value to replace with the current anomaly.

ex: 50,45,30,20
I want to get the mean value and replace it with current value

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 simply define a function that unpack those values and then get the mean of those.

def get_mean(x):
    #split into list of strings
    splited = x.split(',')
    #Transform into numbers
    y = [float(n) for n in splited]
    return sum(y)/len(y)

#Apply on desired column
df['col'] = df['col'].apply(get_mean)
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