简体   繁体   中英

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 | | a | b | c | d | , eg

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 |

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM