1

I am trying to convert the following pandas df

Error   Fore    time    loc qty           con   FA
2960    32863   2086    CFA Ahmedabad   29903   1   90
2960    32863   2087    CFA Ahmedabad   29903   1   90
8090    71453   2086    CFA Bangalore   79543   2   90
8090    71453   2087    CFA Bangalore   79543   2   90    

to

loc              var      2086    2087
CFA Ahmedabad   qty    29903    29903
CFA Ahmedabad   Fore    32863     32863
CFA Ahmedabad   Error   2960      2960
CFA Ahmedabad   con      1       1
CFA Ahmedabad   FA       90      90

I have tried using melt/stack but nothing seems worked

df = pd.melt(df, id_vars='loc', value_vars=var, value_name='time')

this code is not working to get columns as time, any idea will be much appreciated?

1 Answer 1

1

From what I understand, you can use melt() + pivot_table():

m=df.melt(['time','loc']).pivot_table(index=['loc','variable'],columns='time')
print(m)

time                     2086   2087
loc           variable              
CFA Ahmedabad Error      2960   2960
              FA           90     90
              Fore      32863  32863
              con           1      1
              qty       29903  29903
CFA Bangalore Error      8090   8090
              FA           90     90
              Fore      71453  71453
              con           2      2
              qty       79543  79543
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.