Home » excel » python – Pivot/group data from dynamic frame

python – Pivot/group data from dynamic frame

Posted by: admin May 14, 2020 Leave a comment

Questions:

Data is present is in following structure:

s.No| Item Name | Source1 | Price1 | Source 2| Price 2| ....
1   | coffee    | website1| 3.5    | website2| 3.5    |
2   | Tea       | website3| 4.5    | website1| 4.5    |
3   | Soft Drink| website1| 1.5    | website2| 2.5    |

Desired Ouput wanted either using excel or python-pandas

ItemName| website1 | website2| website3
coffee  |   3.5    |    3.5  |   na
Tea     |   4.5    |    na   |   4.5
Soft Drink| 1.5    |    2.5  |   na

Process of tabulating is taking a lot of manual effort and is hugely error prone.
Could someone please help me write code for either excel VB script or with python–pandas please

How to&Answers:

Here’s a solution:

pvt1 = df.pivot(index='Item_Name', columns='Source1', values='Price1').reset_index()
pvt2 = df.pivot(index='Item_Name', columns='Source2', values='Price2').reset_index()

pvt = pd.merge(pvt1, pvt2, on='Item_Name')

which gives us:

    Item_Name  website1_x  website3  website1_y  website2
0  Soft_Drink         1.5       NaN         NaN       2.5
1         Tea         NaN       4.5         4.5       NaN
2      coffee         3.5       NaN         NaN       3.5

Then, this is the code that currently handles website1, but needs to be fixed so it acts on all such columns:

pvt['website1'] = pvt['website1_x'].combine_first(pvt['website1_y'])
pvt.drop(['website1_x', 'website1_y'], axis=1, inplace=True)

Output:

    Item_Name  website3  website2  website1
0  Soft_Drink       NaN       2.5       1.5
1         Tea       4.5       NaN       4.5
2      coffee       NaN       3.5       3.5

Answer:

Using pandas, zip and tuple unpacking:

prices = pd.DataFrame(index=df['Item Name'])
for idx, s_no, item, *row in df.itertuples():
    # print(item, row)
    iters = [iter(row)] * 2
    for source, price in zip(*iters):
        # print(source, price)
        prices.loc[item, source] = price
Item Name website1    website2    website3
coffee        3.5 3.5 na
Tea           4.5 na  4.5
Soft Drink    1.5 2.5 na

If s.No is the index, remove the idx from the for-loop