0

I've this Dataframe

df_stack = pd.DataFrame([['age', 0.0417084448844436],
 ['age', 0.0417084448844436],
 ['sex', 0.0506801187398187],
 ['sex', 0.0506801187398187],
 ['bp', 0.1506801187],
 ['bp', 0.1506801187]])

I need to get this result

     age         bp       sex
0   0.041708    0.15068   0.05068   
1   0.041708    0.15068   0.05068

In SQL we can use a "coalesce" function, that eliminate the null values and pull up the values. Do we have something similar with Pandas? Thank you in advance

Carlos Carvalho
  • 131
  • 1
  • 8
  • 2
    we have [`pivot`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html): `df_stack.assign(k=df_stack.groupby(0).cumcount()).pivot('k',0,1)` , or `df_stack.set_index([df_stack.groupby(0).cumcount(),0])[1].unstack()` : you can check Q10 on the linked Q&A – anky Sep 21 '20 at 14:26
  • Yes i did Check, it's not same issue. besides none of the solutions you provide in your comments works. Returns error messages. – Carlos Carvalho Sep 21 '20 at 14:33
  • 1
    well something like "WTF" is not used here in SO (comment now removed) where we respect everyone. Anyway if you can edit your question with what you have tried thus far (including ,my comments), I can retract the close vote. Until then it looks like a pivot to me, also `df_stack` is just a list of list as you have shown in your question? – anky Sep 21 '20 at 14:45
  • Absolutely. df_stack is just a list of list as you have shown in your question?,no it's a dataframe, Ok that one is on me, but I specifically say, a DataFrame. "I've this Dataframe". Anyway I've rearrange the code. it's a Dataframe not a List. Now, I need to get rid of all null values and push remaining values at same level. And that's not in Q10 – Carlos Carvalho Sep 21 '20 at 14:52
  • I assumed so anyways, if I run your code for `df_stack` and then run `df_stack.set_index([df_stack.groupby(0).cumcount(),0])[1].unstack()` , I get the result what you were looking for.. even the code with `pivot` works for me. so technically it is still a duplicate , also you dont have null values in the example `df_stack` , you may want to edit the example to be clearer – anky Sep 21 '20 at 14:54
  • Can you share your results please? – Carlos Carvalho Sep 21 '20 at 15:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221822/discussion-between-anky-and-carlos-carvalho). – anky Sep 21 '20 at 15:00

0 Answers0