Home > Blockchain >  multiple pandas column by itself to produce an array
multiple pandas column by itself to produce an array

Time:05-18

Given a list (vector) like this:

index    values
item 1   0.05
item 2   0.1
item 3   0.35
...
item n   0.0n

How can one produce an array of the products of each item with itself such that the answer takes this form using a vectorized method (i.e. no for loops):

index    item 1   item 2   item 3 ...  item n
item 1   1*1      1*2      1*3    ...  1*n
item 2   2*1      2*2      
item 3   3*1      3*2     
...          
item n   n*1      n*2             ...  n*n

CodePudding user response:

Use numpy broadcasting for multiple Series each other:

a = df['values'].to_numpy()
i = df.index.to_numpy()
df = pd.DataFrame(a * a[:, None], index=i, columns=i)
print (df)
        item 1  item 2  item 3
item 1  0.0025   0.005  0.0175
item 2  0.0050   0.010  0.0350
item 3  0.0175   0.035  0.1225

CodePudding user response:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,100,size=(10, 1)), columns=list('v'))

df

enter image description here

for i in range(1,5):
    df[f"v_{i}"]=df["v"]*i
  

df

enter image description here

  • Related