Home > Back-end >  How to get values of dataframe that has characteres
How to get values of dataframe that has characteres

Time:11-26

I have a dataframe

df = pd.DataFrame({'col' : [1,2, 10, np.nan, 'a'], 
                   'col2': ['a', 10, 30, 'c',50],
                   'col3': [1,2,3,4,5.0]})

How I obtein about the column col2 a new dataframe with has characters. In this case df_final = ['a', 'c']

I try to verify if not number but this doesn't work for me.

CodePudding user response:

You could use pandas.Series.str.contains in this case by using a regex that does not match numbers. It should be noted that we need to set na argument to False because as per documentation

Specifying na to be False instead of NaN replaces NaN values with False. If Series or Index does not contain NaN values the resultant dtype will be bool, otherwise, an object dtype

df.loc[df['col2'].str.contains(r'[^0-9]', na=False, regex=True)]

   col col2  col3
0    1    a   1.0
3  NaN    c   4.0
  • Related