Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switch columns and rows in dataframe with correct order

I have a DataFrame which I wanted to modify but I’m struggling with the results:

df = pd.DataFrame({"ID": ['2425128168','4055518718', '4055497871', '4055497871'],
                   "To": ['A', 'A', 'B', 'C'],
                   "From": ['D', 'C', 'A', 'A'],
                   "Qty":[10,20,30,40]})
df['Qty'] = df['Qty'].astype(int)

    ID          To From Qty
0   2425128168  A   D   10
1   4055518718  A   C   20
2   4055497871  B   A   30
3   4055497871  C   A   40

The idea is to switch column From into different columns from A to F and write a value from Qty column in correct place, for this example the result should be like that:

    ID          To From Qty A  B C  D  E F
0   2425128168  A   D   10  0  0 0  10 0 0 
1   4055518718  A   C   20  0  0 20 0  0 0
2   4055497871  B   A   30  30 0 0  0  0 0
3   4055497871  C   A   40  40 0 0  0  0 0

I was trying to do that but I get wrong values when the ID are not unique.
This is my solution(wrong):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

grouped = df.pivot(index=['ID','To'], columns='From', values='Qty')
grouped = grouped.fillna(0)

for i in ['A', 'B', 'C', 'D', 'E', 'F']:
    try:
        df[i] = grouped[i].reset_index(drop = True).astype(int)
    except:
        df[i] = 0

df = 

      ID    To  From    Qty A   B   C   D   E   F
0   2425128168  A   D   10  0   0   0   10  0   0
1   4055518718  A   C   20  30  0   0   0   0   0
2   4055497871  B   A   30  40  0   0   0   0   0
3   4055497871  C   A   40  0   0   20  0   0   0

>Solution :

You can use get_dummies and multiply by Qty, then reindex and join:

cols = ['A', 'B', 'C', 'D', 'E', 'F']

out = df.join(pd.get_dummies(df['From'])
                .mul(df['Qty'], axis=0)
                .reindex(columns=cols,
                         fill_value=0)
             )

output:

           ID To From  Qty   A  B   C   D  E  F
0  2425128168  A    D   10   0  0   0  10  0  0
1  4055518718  A    C   20   0  0  20   0  0  0
2  4055497871  B    A   30  30  0   0   0  0  0
3  4055497871  C    A   40  40  0   0   0  0  0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading