ilBat
ilBat

Reputation: 21

Separate numbers from string in DataFrame

I have this dataframe:

df=pd.DataFrame({'A': ['rob12','mark kevin34','john'], 'B' : ['aa','bb','dd'], 'C':[10,34,98]})

and I want to delete the numbers from the first column to get a column like A ['rob', 'mark kevin', 'john']. Any suggestion?

Upvotes: 0

Views: 18

Answers (1)

PacketLoss
PacketLoss

Reputation: 5746

You can use re.sub() and apply() to apply this to column A.

Input:

df
              A   B   C
0         rob12  aa  10
1  mark kevin34  bb  34
2          john  dd  98

Code:

df['A'] = df['A'].apply(lambda x: re.sub("[\d-]", "", x))

On larger data frames you should use map() instead of apply();

df['A'] = df['A'].map(lambda x: re.sub("[\d-]", "", x))

Output:

df
            A   B   C
0         rob  aa  10
1  mark kevin  bb  34
2        john  dd  98

Upvotes: 1

Related Questions