0

Not sure if pivot is the right function.

go from:

df = pd.DataFrame({
    "date" :  ["a", "a", "a", "a", "b", "b", "b","b"],
    "col"  :  ["v", "r", "v", "r", "v", "r", "v","r"],
    "value":  [1,2,3,4,5,6,7,8]

})

to:

df_new = pd.DataFrame({
    "date": ['a', 'a', 'b', 'b'], 
    'v':    [1,3,5,7], 
    'r':    [2,4,6,8]
})

I did not find the precise solution in the question linked.

Here is my solution, similar to point 10 in question:

import pandas as pd 

df = pd.DataFrame({
    "row" :  ["a", "a", "a", "a", "b", "b", "b","b"],
    "col"  :  ["v", "r", "v", "r", "v", "r", "v","r"],
    "val0":  [1,2,3,4,5,6,7,8]

})

df.insert(0, 'count', df.groupby(['row', 'col']).cumcount())
df.pivot(index=['count', "row"], columns='col', values='val0').reset_index().drop("count", axis=1)
Leo
  • 1,176
  • 1
  • 13
  • 33

0 Answers0