Stücke
Stücke

Reputation: 1043

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 |

Upvotes: 0

Views: 48

Answers (2)

jezrael
jezrael

Reputation: 862661

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)

Upvotes: 2

user7864386
user7864386

Reputation:

One option is to use dot:

fr = vec.to_frame()
out = fr.dot(fr.T)

Output:

      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

Upvotes: 3

Related Questions