python - Create Table of all Sums from two Arrays in Pandas
问题描述
I got two arrays:
arr1 = [1,2,3]
arr2 = [5,10]
Now i want to create a Dataframe from the arrays which hold the sum of all combinations:
pd.DataFrame([[6,7,8], [11,12,13]],
columns=['1', '2', '3'],
index=['5', '10'])
1 | 2 | 3 | |
---|---|---|---|
5 | 6 | 7 | 8 |
10 | 11 | 12 | 13 |
I know this can be easily done by iterating over the arrays, but I guess there is a built-in function to accomplish the same but way faster.
I've already looked in the documentation of different functions like the merge function but without success.
解决方案
We can use numpy broadcasting with addition then build the resulting DataFrame by assigning the index
and column
names from the lists:
import numpy as np
import pandas as pd
arr1 = [1, 2, 3]
arr2 = [5, 10]
df = pd.DataFrame(
np.array(arr1) + np.array(arr2)[:, None], index=arr2, columns=arr1
)
Or with add
+ outer
(which works if arr1
and arr2
are lists or arrays):
df = pd.DataFrame(np.add.outer(arr2, arr1), index=arr2, columns=arr1)
*Note if arr1
and arr2
are already arrays (instead of list
) it can just look like:
import numpy as np
import pandas as pd
arr1 = np.array([1, 2, 3])
arr2 = np.array([5, 10])
df = pd.DataFrame(arr1 + arr2[:, None], index=arr2, columns=arr1)
All ways produce df
:
1 2 3
5 6 7 8
10 11 12 13
推荐阅读
- python - 在 Django 中按 2 个相关字段排序时消除重复项
- python - 如何计算具有 2 个参数的变量的数量
- python - 打破一个forloop
- matlab - Matlab 结构:如何将连接的字符串作为字段传递给结构?
- amazon-web-services - 为什么通过 cdk 向 dynamodb 表添加二级索引需要重新创建表?
- javascript - 检查另一个类中的类的实例是否有一些负面影响?
- python-3.x - 如何分隔从文本文件中读取的数据行?客户与他们的订单
- c# - 统一武器开关
- java - java中的计划任务组多线程执行
- graphql - 有没有办法通过查询自省来读取类型上的 GraphQL 指令?