Convert data into a pandas DataFrame and remove some

284 Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER
# 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()  
3
On

Try something like this

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

df = df[df['row_1']!='---'].copy()
df['row_1'] = df['row_1'].astype(float)