Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove single letters from strings in Pandas dataframe

I have a DataFrame where a column is filled with strings. I want to remove any appearance of single letters from the column. So far, I have tried:

df['STRI'] = df['STRI'].map(lambda x: " ".join(x.split() if len(x) >1)

I wish to input ABCD X WYZ and get ABCD WYZ.

like image 478
infinite-rotations Avatar asked Dec 15 '22 01:12

infinite-rotations


1 Answers

Try this:

df['STRI'] = npi['STRI'].str.replace(r'\b\w\b', '').str.replace(r'\s+', ' ')

Eg:

import pandas as pd

df = pd.DataFrame(data=['X ABCD X X WEB X'], columns=['c1'])
print df, '\n'
df.c1 = df.c1.str.replace(r'\b\w\b', '').str.replace(r'\s+', ' ')
print df

Output:

                 c1
0  X ABCD X X WEB X 

           c1
0   ABCD WEB 
like image 131
Mohammad Yusuf Avatar answered Mar 02 '23 23:03

Mohammad Yusuf