How to get values of dataframe that has characteres

Question:

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.

Asked By: philip Gue

||

Answers:

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
Answered By: Anoushiravan R
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.