Skip to content Skip to sidebar Skip to footer

Remove First Character From Pandas Column If The Number 1

The below code removes any dashes in any of the phone number columns. How do I also remove the first character of a phone number in those columns if the phone number begins with 1

Solution 1:

This is an example of using the apply facility to apply functions with non-trivial logic to a column:

for col in cols_to_check:
    df[col] = df[col].apply(lambda x : x[1:] if x.startswith("1") else x)

See also this overview of apply.

Solution 2:

I'd use applymap

Option 1 Use str.replace to just replace '-' with ''. I'm assuming that we can always take last 10 digits.

df[cols_to_check].applymap(lambda x: x.replace('-', '')[-10:])

    PhonephonePhone1012345678901234567890123456789011234567890123456789012345678902123456789012345678901234567890

Option 2 Use re.sub However, if you want to strip all non-digit characters, use the regex module re and do something similar to that in option 1

import re

df[cols_to_check].applymap(lambda x: re.sub(r'\D', '', x)[-10:])

    Phone       phone      Phone1
012345678901234567890123456789011234567890123456789012345678902123456789012345678901234567890

Option 3 We can also use pd.Series.str string accessor. But, we need to collapse into a series first.

df[cols_to_check].stack().str.replace('\D', '').str[-10:].unstack()

    Phone       phone      Phone1
012345678901234567890123456789011234567890123456789012345678902123456789012345678901234567890

Setup

df = pd.DataFrame(dict(
    Phone=['1-123-456-7890', '123-4567890', '11234567890'],
    phone=['1-123-456-7890', '123-4567890', '11234567890'],
    Phone1=['1-123-456-7890', '123-4567890', '11234567890'],
    Other=[1, 2, 3]
))

cols_to_check = ['Phone', 'phone', 'Phone1']

df

   Other           Phone          Phone1           phone
0      1  1-123-456-7890  1-123-456-7890  1-123-456-7890
1      2     123-4567890     123-4567890     123-4567890
2      3     11234567890     11234567890     11234567890

Post a Comment for "Remove First Character From Pandas Column If The Number 1"