Issue
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.
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()
Answered By - 9769953
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.