Home > OS >  Efficiently selecting row of Pandas Dataframe which contains digit
Efficiently selecting row of Pandas Dataframe which contains digit

Time:11-02

How to select rows which contains digits in dataframe. I am using this.

s = set([1,2,3,4,5,6,7,8,9,0])
df["text"].apply(lambda x:any(i in list(x) for i in s))

Trying to get separate the sentences with digits.

CodePudding user response:

Use Series.str.contains with \d for match digit:

df = pd.DataFrame({'text':['text1','text','random 4.88 text']})

m = df["text"].str.contains(r'\d')
print (m)
0     True
1    False
2     True
Name: text, dtype: bool

For filtering:

df1 = df[df["text"].str.contains(r'\d')]

CodePudding user response:

copied from isnumeric

import pandas as pd
s1 = pd.Series(['one', 'one1', '1', ''])
s1.str.isnumeric()

returns

0    False
1    False
2     True
3    False
dtype: bool

to select the elements:

s1[s1.str.isnumeric()]
  • Related