How to create an array according to row and column names using pandas

Question:

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]]
Asked By: raja

||

Answers:

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

Using the numpy function diag you can create a diagonal matrix (list of lists) from a pandas dataframe column.

import pandas as pd
import numpy as np
df = pd.read_csv('data.csv')  
np.diag(df.Value)
Answered By: Olivolja
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.