Home > Mobile >  Dataframe Delete value from specific columns by matching specific KEY in like " .0 " and a
Dataframe Delete value from specific columns by matching specific KEY in like " .0 " and a

Time:08-19

Delete a value from the end and in middle So, first, delete .0 in from the end and then remove "." from whole columns

id Zip              Contact
1  12345.0,67890.0  123.213.1234
2  5.567.4          1212121212.0
3  11111
4  22222.           999.999.9999,7897897897.0

print(out)

id Zip              Contact
1  12345,67890      1232131234
2  55674            1212121212
3  11111
4  22222            9999999999,7897897897

CodePudding user response:

You can use .apply() to apply a function on each column:

def f(ser):
    return (ser[~ser.isna()]
            .astype(str)
            .str.removesuffix(".0")
            .str.replace('\.', '', regex=True))

df[['Zip', 'Contact']] = df[['Zip', 'Contact']].apply(f)
  • Related