0

I've the following pandas dataframe:

>>>    df = pd.DataFrame([
                    [np.nan, 2, 'x', 0], 
                    [3, 4, 'y', 0],
                    [9, 6, 'x', 1],
                    [np.nan, np.nan, 'y', 1]],
                   columns=['ignore', 'value', 'col', 'row'])


>>> df
   ignore  value col  row
0     NaN    2.0   x    0
1     3.0    4.0   y    0
2     9.0    6.0   x    1
3     NaN    NaN   y    1

I want to be able to convert it to something like the following:

   x      y 
0  2.0    4.0
1  6.0    NaN 

Is it possible using pivot or multi-index or anything else? Or the only possible solution is looping through individual values?

orak
  • 2,279
  • 6
  • 28
  • 54

1 Answers1

0

You can probably use pd.crosstab:

pd.crosstab(columns=df.col,values=df.value,index=df.row, aggfunc=np.min)
Out[29]: 
col    x    y
row          
0    2.0  4.0
1    6.0  NaN
Allen Qin
  • 18,332
  • 6
  • 47
  • 59