1

While working with pandas Dataframe object and using the conditional selection from the DataFrame it gives an error as follows, while i see while looking through some of the videos where same works on Jupiter notebook.

However, it clearly states the error below but i did not found a way around to fix it even though i searched upon on various SO post but did not get identical error solution

TypeError: '>' not supported between instances of 'str' and 'int'

Below is the code content which i'm using with puthon3.6.

import pandas as pd
import numpy as np

df  = pd.DataFrame({'coln1': ['1', '2', '3'],
                    'coln2': ['111', '222', '111'],
                    'coln3': ['aaa', 'bbb', 'ccc']})

print(df[df['coln1']>2])

Any help or direction are much appreciated, while i'm still looking around myself.

jpp
  • 159,742
  • 34
  • 281
  • 339
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53

1 Answers1

2

Convert column to numeric by Series.astype:

print(df[df['coln1'].astype(int)>2])

More general solution if some non numeric values, which are converted to NaNs by to_numeric and parameter errors='coerce':

print(df[pd.to_numeric(df['coln1'], errors='coerce')>2])
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252