0

I have a dataframe like the following.

i j element
0 0 1
0 1 2
0 2 3
1 0 4
1 1 5
1 2 6
2 0 7
2 1 8
2 2 9

How can I convert it to the 3*3 array below?

1 2 3
4 5 6
7 8 9
Houhouhia
  • 35
  • 6
  • Does this answer your question? [Convert pandas dataframe to NumPy array](https://stackoverflow.com/questions/13187778/convert-pandas-dataframe-to-numpy-array) – msanford Nov 04 '22 at 15:20

2 Answers2

2

Assuming that the dataframe is called df, one can use pandas.DataFrame.pivot as follows, with .to_numpy() (recommended) or .values as follows

array = df.pivot(index='i', columns='j', values='element').to_numpy()

# or

array = df.pivot(index='i', columns='j', values='element').values

[Out]:

array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]], dtype=int64)
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
0

If you transform your dataframe into three lists where the first is containing "i" values, the second - j and the third is data, you can create NumPy array "manually":

i, j, v = zip(*[x for x in df.itertuples(index=False, name=None)])
arr = np.zeros(df.shape)
arr[i, j] = v
svfat
  • 3,273
  • 1
  • 15
  • 34