Mohit Rai Mohit Rai - 1 month ago 9
Python Question

Comparing cell values with a integer in pandas giving typeerror

I have been trying to compare values of each cell in a row with an integer like so:

df.loc[df['A'] <= 14, 'A']


All rows whose values are less than or equal to 14, but it shows an error like:


TypeError : '<=' is not supported between instances of str and int


Answer Source

You need replace , to empty space and convert to int:

df = pd.DataFrame({'A':['1,473','1,473','1,4', '1,2'],
                   'B':[2,4,5,5]})
print (df)
       A  B
0  1,473  2
1  1,473  4
2    1,4  5
3    1,2  5

df['A'] = df['A'].str.replace(',', '').astype(int)
s = df.loc[df['A'] <= 14, 'A']
print (s)
2    14
3    12
Name: A, dtype: int32

If use read_csv for DataFrame add parameter thousands=',':

df = pd.read_csv(file, thousands=',', sep=';')

Sample:

import pandas as pd
from pandas.compat import StringIO

temp=u"""A;B
1,473;2
1,473;4
1,4;5
1,2;5"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), thousands=',', sep=';')

print (df)
      A  B
0  1473  2
1  1473  4
2    14  5
3    12  5

s = df.loc[df['A'] <= 14, 'A']
print (s)
2    14
3    12
Name: A, dtype: int64