1

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
1

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.