I have a pandas dataframe like this:
order_id buyer_id phone_no
611 261 9920570003
681 261 9321613595
707 261 9768270700
707 261 9768270700
707 261 9768270700
708 261 9820895896
710 261 7208615775
710 261 7208615775
710 261 7208615775
711 261 9920986486
800 234 Null
801 256 Null
803 289 Null
I have to replace the buyer_id column as follows:
order_id buyer_id phone_no
611 261_01 9920570003
681 261_02 9321613595
707 261_03 9768270700
707 261_03 9768270700
707 261_03 9768270700
708 261_04 9820895896
710 261_05 7208615775
710 261_05 7208615775
710 261_05 7208615775
711 261_06 9920986486
800 234 Null
801 256 Null
803 289 Null
So if the phone no is same it should treat it as same buyer else it should add new series to 261. I want only 261 buyer_id
to be renamed other rows should be the same. Because I am treating orders coming from phone as 261
I am able to add series in 261
buyer_id with following code:
for i in range((len(phone_orders):
print '261_%d' %i
segments_data['buyer_id']
phone_orders
contains all the phone orders.
But I didn't get how to replace the buyer_id
column with desired output
df['buyer_id'] = '261_' + (df['phone_no'] !=
df['phone_no'].shift()).cumsum().map("{:02}".format)
buyer_id phone_no
261_01 9920570003
261_02 9321613595
261_03 9768270700
261_03 9768270700
261_03 9768270700
261_04 9820895896
261_05 7208615775
261_05 7208615775
261_05 7208615775
261_06 9920986486
261_07 9768270700
261_07 9768270700
261_07 9768270700
261_08 9820895896
261_09 7208615775
261_09 7208615775
261_09 7208615775
So 7208615775
phone_no should be 261_05
but it is giving 261_09
.