2

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]]
1
  • I think your row/col are swapped Commented Oct 25, 2022 at 12:09

2 Answers 2

1

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

Comments

1

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)

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.