Home > other >  Empty pivot table with pandas data frames
Empty pivot table with pandas data frames

Time:09-21

I have enter image description here

I'd like to pivot column values, so I'm trying:

pivot = pd.pivot_table(data, values='D', index=['A', 'B', 'C', 'E'], columns=['col'])

I'd expect something like

enter image description here

Instead, I get an empty table. Can anyone help here?

CodePudding user response:

pivot = pd.pivot(data, values='D', index=['A', 'B', 'C', 'E'], columns=['col'])

If you want to reset the index you can use pivot.reset_index() which returns:

col   A    B   C    E  10
0    a1  NaN  c1   e1   0
1    a1  NaN  c1   e2   0
2    a1  NaN  c1   e3   0
3    a1  NaN  c1   e4   0
4    a1  NaN  c1   e5   0
5    a2   bb  c2  NaN   0
6    a2   bc  c2  NaN   0
7    a2   bd  c2  NaN   0
8    a2   be  c2  NaN   0
9    a2   bf  c2  NaN   0
  • Related