Home > database >  How to identify and remove all the special characters from the data frame
How to identify and remove all the special characters from the data frame

Time:08-24

col1
Ntwk Lane 0 cannot on high operational\n
TX_PWR ALARM. TX_PWR also fluctuates over time (found Tx power dropped to -2dBm also raises TX_PWR_LO_ALRM
module report ASIC_PLL_REF_CLK_FREQ_ERR(20008=0x800000) and HOST_REF_PLL_2(20014=0x2)

I want to remove all the special characters from the column how to do that. I need only alphabets rest I need to remove

CodePudding user response:

You can use regular expression:

import re
df['col2'] = df['col1'].apply(lambda x: re.compile('[^a-zA-Z]').sub('', x))

As suggested by @9769953

df['col2'] = df['col1'].str.replace('[^a-zA-Z]', '', regex=True)

is also a much cleaner approach. Same performance, but cleaner.

  • Related