Archit
Archit

Reputation: 588

Remove a number from a particular location from a column in pandas dataframe

I have column in a pandas dataframe df

import pandas as pd
s = {'id': [47035,460,23045,87068,8007,78096],
 'st': ['a', 'a', 'd', 'e', 'f', 'a']}
df = pd.DataFrame(s)

I want to remove the 0(or any other number if present) which is at the third location only in the column id. how can I do the same ?

So after removal, my values in the column id should become 4735, 46, 2345, 8768, 807, 7896.

Upvotes: 1

Views: 75

Answers (4)

Rajat Jain
Rajat Jain

Reputation: 2032

This solution also works:

df['id'].astype(str).str.replace(r"^(?P<one>..).", lambda x: x.group("one") )

EDIT: Group named "one" picks up first two integers and keeps them in the final replacement although the third integer is picked gets removed.

Upvotes: 1

Andy L.
Andy L.

Reputation: 25239

use str.slice_replace as follows:

df.id.astype(str).str.slice_replace(2, 3, '')

Out[422]:
0    4735
1      46
2    2345
3    8768
4     807
5    7896
Name: id, dtype: object

Upvotes: 4

BENY
BENY

Reputation: 323236

IIUC

df.id.str[:2]+df.id.str[2].where(df.id.str[2]==0,'')+df.id.str[3:]
0    4735
1      46
2    2345
3    8768
4     807
5    7896
Name: id, dtype: object

Upvotes: 1

It_is_Chris
It_is_Chris

Reputation: 14103

One option is to convert them to a string and remove the third character then convert back to int:

s = {'id': [47035,460,23045,87068,8007,78096],
 'st': ['a', 'a', 'd', 'e', 'f', 'a']}
df = pd.DataFrame(s)

# convert to sting and strip away the middle third character then concat
df['id'] = (df['id'].astype(str).str[:2] + df['id'].astype(str).str[3:]).astype(int)

     id st
0  4735  a
1    46  a
2  2345  d
3  8768  e
4   807  f
5  7896  a

Upvotes: 1

Related Questions