notilas
notilas

Reputation: 2485

Python: Pandas, Dataframe, Convert 1column data into 2D data format

This is Pandas dataframe I want to convert 1D data into 2D array form

How do I convert from

  'A'  'B'  'C'
1  10   11   a  
2  10   12   b
3  10   13   c 
4  20   11   d  
5  20   12   e
6  20   13   f

to this 2d array as the following

   11 12 13
10  a  b  c 
20  d  e  f

Upvotes: 2

Views: 1502

Answers (2)

Calvin Cheng
Calvin Cheng

Reputation: 36506

You can also use panels to help you do this pivot. Like this:-

In [86]: panel = df.set_index(['A', 'B']).sortlevel(0).to_panel()

In [87]: panel["C"]
Out[87]: 
B  11 12 13
A          
10  a  b  c
20  d  e  f

Which gives you the same result as Sebastian's answer above.

Upvotes: 2

jfs
jfs

Reputation: 414129

>>> df.pivot('A', 'B', 'C')
  B  11  12  13
A              
10   a   b   c 
20   d   e   f 

Where df is:

>>> df = DataFrame(dict(A=[10]*3+[20]*3, B=range(11, 14)*2, C=list('abcdef')))
>>> df
   A   B   C
0  10  11  a
1  10  12  b
2  10  13  c
3  20  11  d
4  20  12  e
5  20  13  f

See Reshaping and Pivot Tables

Upvotes: 6

Related Questions