create matrix structure using pandas
12,391
Solution 1
numpy as a faster alternative
pd.DataFrame(np.outer(df, df), df.index, df.index)
Timing
Given sample
30,000 rows
df = pd.concat([df for _ in range(10000)], ignore_index=True)
Solution 2
You want to do the math between a vector and its tranposition. Transpose with .T
and apply the matrix dot
function between the two dataframes.
df = df.set_index('CODE')
df.T
Out[10]:
CODE A B C
COEFFICIENT 0.5 0.4 0.3
df.dot(df.T)
Out[11]:
CODE A B C
CODE
A 0.25 0.20 0.15
B 0.20 0.16 0.12
C 0.15 0.12 0.09
Author by
dataviz
Updated on July 30, 2022Comments
-
dataviz almost 2 years
I have loaded the below CSV file containing code and coefficient data into the below dataframe df:
CODE|COEFFICIENT A|0.5 B|0.4 C|0.3 import pandas as pd import numpy as np df= pd.read_csv('cod_coeff.csv', delimiter='|', encoding="utf-8-sig")
giving
ITEM COEFFICIENT 0 A 0.5 1 B 0.4 2 C 0.3
From the above dataframe, I need to create a final dataframe as below which has a matrix structure with the product of the coefficients:
A B C A 0.25 0.2 0.15 B 0.2 0.16 0.12 C 0.15 0.12 0.09
I am using
np.multiply
but I am not successful in producing the result.