Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pd.Series to pd.DataFrame while multiplying each element with each other element

Assuming I have a vector of the form | a | b | c | d |, e.g.

vec = pd.Series([0.3,0.2,0.2,0.3])

What’s a quick and elegant way to build a pd.DataFrame of the form:

| a*a | a*b | a*c | a*d |
| b*a | b*b | b*c | b*d |
| c*a | c*b | c*c | c*d |
| d*a | d*b | d*c | d*d |

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use numpy broadcasting:

vec = pd.Series([0.3,0.2,0.2,0.3])

a = vec.to_numpy()

df = pd.DataFrame(a * a[:, None], index=vec.index, columns=vec.index)
print (df)
      0     1     2     3
0  0.09  0.06  0.06  0.09
1  0.06  0.04  0.04  0.06
2  0.06  0.04  0.04  0.06
3  0.09  0.06  0.06  0.09

Or numpy.outer:

df = pd.DataFrame(np.outer(vec, vec), index=vec.index, columns=vec.index)
print (df)
      0     1     2     3
0  0.09  0.06  0.06  0.09
1  0.06  0.04  0.04  0.06
2  0.06  0.04  0.04  0.06
3  0.09  0.06  0.06  0.09

Performance (if is important like mentioned @enke in comments):

np.random.seed(2022)
vec = pd.Series(np.random.rand(10000))

print (vec)


In [39]: %%timeit
    ...: fr = vec.to_frame()
    ...: out = fr.dot(fr.T)
    ...: 
386 ms ± 13.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [40]: %%timeit
    ...: pd.DataFrame(np.outer(vec, vec), index=vec.index, columns=vec.index)
    ...: 
    ...: 
351 ms ± 2.62 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [41]: %%timeit
    ...: a = vec.to_numpy()
    ...: 
    ...: df = pd.DataFrame(a * a[:, None], index=vec.index, columns=vec.index)
    ...: 
293 ms ± 4.22 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading