Issue
for example i have a data like csv file:
x(col) y(row) Value
0 0 5
3 1 10
2 2 2
1 3 6
output:
[[5,0,0,0],
[0,10,0,0],
[0, 0,2,0],
[0, 0,0,6]]
Solution
You can use a pivot_table
:
a = (df
.pivot_table(index='y(row)', columns='x(col)',
values='Value', fill_value=0)
.reindex(index=range(df['y(row)'].max()+1),
columns=range(df['x(col)'].max()+1))
.to_numpy()
)
Or numpy indexing:
a = np.zeros((df['y(row)'].max()+1, df['y(row)'].max()+1), dtype=df['Value'].dtype)
a[df['y(row)'], df['x(col)']] = df['Value']
print(a)
output:
array([[ 5, 0, 0, 0],
[ 0, 0, 0, 10],
[ 0, 0, 2, 0],
[ 0, 6, 0, 0]])
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.