Home > Software design >  Remove substring from pandas dataframe if it contain digit
Remove substring from pandas dataframe if it contain digit

Time:08-29

I have pandas dataframe column as follow

col
ABC xyz123
ABC
GHE a12bc
ABC GHE

I want to remove the substring that contain digit. First, I can split the string based on space

df['col'].apply(lambda x:x.split(' '))

Now i got list in each cell. My question is that how can i remove the string which has digit in it

The final output should be

col
ABC
ABC
GHE
ABC GHE

CodePudding user response:

The lambda you want is:

lambda x: ' '.join(part for part in x.split() if not any(map(str.isdigit, part)))

CodePudding user response:

Split the string values on space, then apply a function to filter out the values that contain digits.

(df['col'].str.split()
 .apply(lambda x: ' '.join(filter(lambda y: not any(i.isdigit() for i in y), x))))

0        ABC
1        ABC
2        GHE
3    ABC GHE
Name: col, dtype: object
  • Related