Home > Back-end >  How to create an array according to row and column names using pandas
How to create an array according to row and column names using pandas

Time:10-25

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]]

CodePudding user response:

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]])

CodePudding user response:

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