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

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

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

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')
print(data.dtypes)

nan_counter = data.isna().sum()
print(nan_counter)
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)

Error:

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)
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