Issue
I have a table look like this in DataFrame:
couriers | delivery | price_list |
---|---|---|
Alfred Locker | 2 day(s) | HKD $20 |
Hongkong Post | 2 day(s) | HKD $50 |
ZTO Express (To-Point) | 2 day(s) | HKD $20 |
Zeek2Door (To Home) | 2 day(s) | HKD $40 |
Kerry Express | 2 day(s) | HKD $30 |
I would like to rearrange the table like below:
couriers_1 | delivery_1 | price_list_1 | couriers_2 | delivery_2 | price_list_2 | couriers_3 | delivery_3 | price_list_3 | couriers_4 | delivery_4 | price_list_4 | couriers_5 | delivery_5 | price_list_5 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Alfred Locker | 2 day(s) | HKD $20 | Hongkong Post | 2 day(s) | HKD $50 | ZTO Express (To-Point) | 2 day(s) | HKD $20 | Zeek2Door (To Home) | 2 day(s) | HKD $40 | Kerry Express | 2 day(s) | HKD $30 |
How can I use Pandas to solve this? I can't figure it out!
Data:
{'couriers': ['Alfred Locker',
'Hongkong Post',
'ZTO Express (To-Point)',
'Zeek2Door (To Home)',
'Kerry Express'],
'delivery': ['2 day(s)', '2 day(s)', '2 day(s)', '2 day(s)', '2 day(s)'],
'price_list': ['HKD $20', 'HKD $50', 'HKD $20', 'HKD $40', 'HKD $30']}
Solution
I think you could unstack
+ convert MultiIndex to a plain index + transpose
:
s = df.unstack().sort_index(level=1)
s.index = [f'{x}_{y+1}' for x,y in s.index]
out = s.to_frame().T
Output:
couriers_1 delivery_1 price_list_1 couriers_2 delivery_2 price_list_2 couriers_3 delivery_3 price_list_3 couriers_4 delivery_4 price_list_4 couriers_5 delivery_5 price_list_5
0 Alfred Locker 2 day(s) HKD $20 Hongkong Post 2 day(s) HKD $50 ZTO Express (To-Point) 2 day(s) HKD $20 Zeek2Door (To Home) 2 day(s) HKD $40 Kerry Express 2 day(s) HKD $30
Answered By - enke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.