Home > Back-end >  How to select rows with at least one categorical value in pandas DataFrame
How to select rows with at least one categorical value in pandas DataFrame

Time:02-12

How I can retrieve indexes of the rows, in a pandas DataFrame (df), with "object" type (dtype=='O') in at least one column of the DataFrame?

What I would like to do in practice is to create a new dataframe (numeric_df) with only the numeric values of a source dataframe (df).

CodePudding user response:

Use:

df = pd.DataFrame({'object col':[1,'a',25]})
df[df['object col'].apply(lambda x: type(x))==int]

result:

enter image description here

Based on the comment:

df = pd.DataFrame({'object col':[1,'a',25], 'object col2':['a','a',25]})
def check(row):
  for col in row:
    if type(col)==str:
      return False
  return True
df[df.apply(check, axis = 1)]

result:

enter image description here

  • Related