0

I'm starting with a dataframe as below (df2):

- Day Sorbent Gabapentin 1
0 1 AX 0.0
1 1 AX 0.0
2 1 AX 0.0
3 1 ABN 0.0
4 1 ABN 0.0
.. ... ... ...
84 30 Dex 4.4

I want to end up with a data frame as below:

Day ABN AX Dex ENV WCX
0 1 0.0 0.0 0.0 0.0 0.0
1 1 0.0 0.0 0.0 0.0 0.0
2 1 0.0 0.0 0.0 0.0 0.0
3 4 0.0 0.3 0.1 0.0 0.1
4 4 0.0 0.3 0.1 0.1 0.2
... ... ... ... ... ..
30 30 0.0 3.3 4.4 8.5 10.1

So I have tried a variety of input and the closest I can get is:

df3 = df2.set_index(['Day', 'Sorbent'], append = True)
print(df3.unstack(level = -1))

Which gives:

    Gabapentin 1                   
Sorbent ABN AX Dex ENV WCX
Day
0 1 NaN 0.0 NaN NaN
1 1 NaN 0.0 NaN NaN
2 1 NaN 0.0 NaN NaN
3 1 0.0 NaN NaN NaN
4 1 0.0 NaN NaN NaN
... ... ... ... ...
84 30 NaN NaN 0.0 NaN
85 30 NaN NaN 0.0 NaN
86 30 NaN NaN NaN 0.0
87 30 NaN NaN NaN 0.0
88 30 NaN NaN NaN 0.0

I've tried pivots etc. but I don't want the data averaged out.

If anyone has any ideas I'm sure I'm just missing something really silly but I would appreciate any help.

Shaido
  • 27,497
  • 23
  • 70
  • 73
JClokey
  • 1
  • 1
  • Apologies, I did apply formatting but I'm not sure why it isn't posting with the formatting – JClokey Apr 28 '22 at 06:01
  • Looks to be due to a missing empty line before the table (although it looked correct in the edit preview). I fixed it but the last table looks incorrect, you can [edit] the question to fix it. – Shaido Apr 28 '22 at 06:06

0 Answers0