1

I had a problem when I was making a dataset

I want to reshape pandas dataframe as a array as below.

If I made a pandas df like this,

import pandas as pd
import numpy as np
index = [1, 2, 3, 4, 5, 6, 7]
a = ['aa', 'bb', 'cc', 'cc', 'aa', 'aa', 'bb']
b = ['123E2', 'BD23', '4124', '1535', 'A535', 'CCD35', '34EF3']
df = pd.DataFrame({'A': a, 'B': b}, index=index)
df

Output
    A   B
1   aa  123E2
2   bb  BD23
3   cc  4124
4   cc  1535
5   aa  A535
6   aa  CCD35
7   bb  34EF3

df1 = df.sort_values(by='A')
df1

Output
    A   B
1   aa  123E2
5   aa  A535
6   aa  CCD35
2   bb  BD23
7   bb  34EF3
3   cc  4124
4   cc  1535

Suppose 'A' means a person who takes a class, and 'B' means a class

I want to make it like that (reshape)

How can I do that ??

1   aa  [123E2, A535, CCD35]
2   bb  [BD23, 34EF3]
3   cc  [4124, 1535]

Thanks !

Logica
  • 977
  • 4
  • 16
mslee
  • 11
  • 1
  • Yeeeesssss Thanks!!! :) – mslee Mar 09 '20 at 05:11
  • Your question is not quite clear. Do you want to convert your DataFrame into np.ndarray or you just want to operate your DataFrame so that it looks like the way you want (the output is still a DataFrame)? – nngeek Mar 09 '20 at 05:12
  • @nngeek I just want to reshape original data no matter it is a DataFrame or np.darray Thanks ! – mslee Mar 09 '20 at 23:16

0 Answers0