Nabih Bawazir
Nabih Bawazir

Reputation: 7275

How to group phone number with and without country code

I am trying to detect phone number, my country code is +62 but some phone manufacturer or operator use 0 and +62, after query and pivoting I get pivoted data. But, the pivoted data is out of context

Here's the pivoted data

Id    +623684682   03684682   +623684684   03684684
1              1          0            1          1
2              1          1            2          1

Here's what I need to group, but I don't want to group manually (+623684682 and 03684682 is same, etc)

Id      03684682   03684684
1              1          2
2              2          3

Upvotes: 1

Views: 62

Answers (1)

jezrael
jezrael

Reputation: 863531

I think need replace with aggregate sum:

df = df.groupby(lambda x: x.replace('+62','0'), axis=1).sum()

Or replace columns names and sum:

df.columns = df.columns.str.replace('\+62','0')
df = df.sum(level=0, axis=1)

print (df)
    03684682  03684684
Id                    
1          1         2
2          2         3

Upvotes: 4

Related Questions