Unexpected dtype in .csv file

I am trying to tidy up some data that I imported from a CSV file. I am trying to drop all rows with a negative value in the ‘Active Power’ coloumn.

I am using Python 3.

Imported the csv file using pd.read_csv()
Specified dtype as float using .astype('float')
When then trying to drop the appropriate cells I get

TypeError: '<' not supported between instances of 'str' and 'float'

Here is the code:

data = pd.read_csv('data.csv')
data['Control'] = data['Control'].astype('float')
data['Power'] = data['Power'].astype('float')

nan_counter = data.isna().sum()
Control  float64
Power    float64
dtype: object

Control      0
Power        0
dtype: int64

Problematic Part:

data = data.drop(data[data["Power" < 0.0]].index, inplace = True)


TypeError: '<' not supported between instances of 'str' and 'float'

I get the same error (str vs int, in that case) if I set the data type as int and use <0 instead of <0.0

>Solution :

You are trying to compare the column name "Power" with the float value 0.0, which results in a TypeError (you can’t use < to compare a string to a float). You need to compare the values in the "Power" column with 0.0 to filter the rows with negative values:

data.drop(data[data['Power'] < 0.0].index, inplace=True)

Leave a Reply