Home > Mobile >  Convert a dataframe to an array
Convert a dataframe to an array

Time:11-05

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

CodePudding user response:

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)

CodePudding user response:

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
  • Related