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

Convert data into a pandas DataFrame and remove some

I’ve a data set of measurement to convert into dataframe of float values. But sometimes the machine does not measurement and set a "—" character which give pandas.to_numeric ValueError.
For a reduced exemple here, my question is how to convert to float a hole columns and delete where I’ve a string "—" character set :

data = {'row_1': ["3.0", "2.4", "---", "0.0"], 'row_2': ['a', 'b', 'c', 'd']}
df = pandas.DataFrame.from_dict(data)

How to delete the entire third line and convert the row_1 values to float ? Thanks.

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 :

# Convert to floating point, but first make sure triple dashes can be interpreted as NaNs
df['row_1'] = df['row_1'].replace('---', 'NaN').astype(float)

# drop rows with NaNs
df = df.dropna()  
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