1

I have a dataframe that looks like the following where I have different number of case and 3 unique values for val.

df
     case    val
0     0       x
1     0       y 
2     0       z
3     1       x 
4     1       z
5     2       y

Now I would like to have a dataframe with a single row for each case and numbered columns for val. I would like something like this:

df1
      case     val0    val1    val2
0       0        x       y      z
1       1        x      nan     z
2       2       nan      y     nan
emax
  • 6,965
  • 19
  • 74
  • 141
  • 1
    For a given case, how to you determine among all the val values which ones will be val0, val1, etc. ? – Adrien Jul 27 '21 at 12:30
  • Does this answer your question? [How to pivot a dataframe?](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – Anurag Dabas Jul 27 '21 at 12:38

1 Answers1

2

Use pivot:

out = df.pivot(index='case', columns='val', values='val')
out.columns = [f'val{i}' for i in range(len(out.columns))]
>>> out
     val0 val1 val2
case               
0       x    y    z
1       x  NaN    z
2     NaN    y  NaN
Corralien
  • 109,409
  • 8
  • 28
  • 52