Matthew
Matthew

Reputation: 433

Optimal way of Reshaping Pandas Dataframe

I have a one dimensional dataframe setup like this:

[A1,B1,C1,A2,B2,C2,A3,B3,C3,A4,B4,C4,A5,B5,C5,A6,B6,C6]

In the my program A1,...,C6 will be numbers read from a csv. I would like to reshape it into a 2d dataframe like this:

[A1,B1,C1]
[A2,B2,C2]
[A3,B3,C3]
[A4,B4,C4]
[A5,B5,C5]
[A6,B6,C6]

I could make this using loops but it will slow the program down a lot since I would be making this transformation many times. What is the optimal command for reshaping data this way? I looked through a bunch of the reshape dataframe questions but couldn't find anything specific to this. Thanks in advance.

Upvotes: 3

Views: 94

Answers (4)

Samuel Nde
Samuel Nde

Reputation: 2743

I would reshape the array and ensure that the order argument is set to "A"

mylist = np.array(['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'a3', 'b3', 'c3', 'a4', 'b4', 'c4', 'a5','b5', 'c5', 'a6', 'b6', 'c6'])

reshapedList = mylist.reshape((6, 3), order = 'A')

print(mylist)

>>>  ['a1' 'b1' 'c1' 'a2' 'b2' 'c2' 'a3' 'b3' 'c3' 'a4' 'b4' 'c4' 'a5' 'b5' 'c5' 'a6' 'b6' 'c6']


print(reshapedList)

[['a1' 'b1' 'c1']
 ['a2' 'b2' 'c2']
 ['a3' 'b3' 'c3']
 ['a4' 'b4' 'c4']
 ['a5' 'b5' 'c5']
 ['a6' 'b6' 'c6']]

If you want a pandas dataframe, you can get it as follows.

df = pd.DataFrame(mylist.reshape((6, 3), order = 'A'), columns = list('ABC'))

>>> df

    A   B   C
0   a1  b1  c1
1   a2  b2  c2
2   a3  b3  c3
3   a4  b4  c4
4   a5  b5  c5
5   a6  b6  c6

Note: It is important that you take sometime to check the differences between dataframe and array. Your question spoke of dataframe but what you really meant was array.

Upvotes: 1

Alexander
Alexander

Reputation: 109626

Using a stride (step) when parsing the list, assuming the data is in the format you provided.

s = [A1,B1,C1,A2,B2,C2,A3,B3,C3,A4,B4,C4,A5,B5,C5,A6,B6,C6]

Note that if s is initially a dataframe with one row and 18 columns, you can convert it to a list via:

s = s.T.iloc[:, 0].tolist()

Then convert the result into a dataframe of your chosen dimension via:

df = pd.DataFrame({'A': s[::3], 'B': s[1::3], 'C': s[2::3]})

More generally:

s = range(18)

cols = 3
>>> pd.DataFrame([s[n:(n + cols)] for n in range(0, len(s), cols)])
    0   1   2
0   0   1   2
1   3   4   5
2   6   7   8
3   9  10  11
4  12  13  14
5  15  16  17

Upvotes: 3

BENY
BENY

Reputation: 323326

Using list split

[s[x:x+3] for x in range(0, len(s),3)]
Out[1151]: 
[['A1', 'B1', 'C1'],
 ['A2', 'B2', 'C2'],
 ['A3', 'B3', 'C3'],
 ['A4', 'B4', 'C4'],
 ['A5', 'B5', 'C5'],
 ['A6', 'B6', 'C6']]

#pd.DataFrame([s[x:x+3] for x in range(0, len(s),3)])

Upvotes: 1

piRSquared
piRSquared

Reputation: 294488

Setup

s = "A1,B1,C1,A2,B2,C2,A3,B3,C3,A4,B4,C4,A5,B5,C5,A6,B6,C6".split(',')

Using Numpy

pd.DataFrame(np.array(s).reshape(-1, 3))

    0   1   2
0  A1  B1  C1
1  A2  B2  C2
2  A3  B3  C3
3  A4  B4  C4
4  A5  B5  C5
5  A6  B6  C6

Iterator shenanigans

pd.DataFrame([*zip(*[iter(s)]*3)])

    0   1   2
0  A1  B1  C1
1  A2  B2  C2
2  A3  B3  C3
3  A4  B4  C4
4  A5  B5  C5
5  A6  B6  C6

Upvotes: 6

Related Questions