gpt4 book ai didi

python - 使用 Python 的组合方法

转载 作者:行者123 更新时间:2023-11-28 22:17:44 27 4
gpt4 key购买 nike

我是 Python 的新手,我正在尝试用两组不同的数据解决问题。我使用 pandas.read_csv 创建了两个数据框 s 和 c

S

  Spread   per1   per2   per3          acc
0 4.50% 1.26% 0.35% 0.50% 0.60%
1 4.45% 1.29% 0.35% 0.50% 0.60%
2 4.40% 1.31% 0.35% 0.50% 0.60%
3 4.35% 1.34% 0.35% 0.50% 0.60%
4 4.30% 1.37% 0.35% 0.50% 0.60%

c

     Cer     OO
0 1.00% 0.65%
1 1.05% 0.68%
2 1.10% 0.71%
3 1.15% 0.73%
4 1.20% 0.76%

所以我想要做的是,对于来自 c 的 Cer 和 OO 的每个组合,我想要填充来自 s 的所有记录。 (不确定这是否是解释它的好方法)。请看下面的例子

    Spread  per1    per2    per3    acc     Cer     OO
0 4.50% 1.26% 0.35% 0.50% 0.60% 1.00% 0.65%
1 4.45% 1.29% 0.35% 0.50% 0.60% 1.00% 0.65%
2 4.40% 1.31% 0.35% 0.50% 0.60% 1.00% 0.65%
3 4.35% 1.34% 0.35% 0.50% 0.60% 1.00% 0.65%
4 4.30% 1.37% 0.35% 0.50% 0.60% 1.00% 0.65%
5 4.50% 1.26% 0.35% 0.50% 0.60% 1.05% 0.68%
6 4.45% 1.29% 0.35% 0.50% 0.60% 1.05% 0.68%
7 4.40% 1.31% 0.35% 0.50% 0.60% 1.05% 0.68%
8 4.35% 1.34% 0.35% 0.50% 0.60% 1.05% 0.68%
9 4.30% 1.37% 0.35% 0.50% 0.60% 1.05% 0.68%

我读到 pandas 和/或 itertools 可以帮助我进行此类分析?混淆是在尝试从 s 为数据帧 c 的两个字段的每个组合创建所有记录时。

同样,任何有关文档和/或见解的帮助都会很棒。谢谢

最佳答案

使用笛卡尔连接 merge 和一个临时键:

S.assign(key=1).merge(c.assign(key=1), on='key').drop('key', axis=1)

输出:

   Spread   per1   per2   per3    acc    Cer     OO
0 4.50% 1.26% 0.35% 0.50% 0.60% 1.00% 0.65%
1 4.50% 1.26% 0.35% 0.50% 0.60% 1.05% 0.68%
2 4.50% 1.26% 0.35% 0.50% 0.60% 1.10% 0.71%
3 4.50% 1.26% 0.35% 0.50% 0.60% 1.15% 0.73%
4 4.50% 1.26% 0.35% 0.50% 0.60% 1.20% 0.76%
5 4.45% 1.29% 0.35% 0.50% 0.60% 1.00% 0.65%
6 4.45% 1.29% 0.35% 0.50% 0.60% 1.05% 0.68%
7 4.45% 1.29% 0.35% 0.50% 0.60% 1.10% 0.71%
8 4.45% 1.29% 0.35% 0.50% 0.60% 1.15% 0.73%
9 4.45% 1.29% 0.35% 0.50% 0.60% 1.20% 0.76%
10 4.40% 1.31% 0.35% 0.50% 0.60% 1.00% 0.65%
11 4.40% 1.31% 0.35% 0.50% 0.60% 1.05% 0.68%
12 4.40% 1.31% 0.35% 0.50% 0.60% 1.10% 0.71%
13 4.40% 1.31% 0.35% 0.50% 0.60% 1.15% 0.73%
14 4.40% 1.31% 0.35% 0.50% 0.60% 1.20% 0.76%
15 4.35% 1.34% 0.35% 0.50% 0.60% 1.00% 0.65%
16 4.35% 1.34% 0.35% 0.50% 0.60% 1.05% 0.68%
17 4.35% 1.34% 0.35% 0.50% 0.60% 1.10% 0.71%
18 4.35% 1.34% 0.35% 0.50% 0.60% 1.15% 0.73%
19 4.35% 1.34% 0.35% 0.50% 0.60% 1.20% 0.76%
20 4.30% 1.37% 0.35% 0.50% 0.60% 1.00% 0.65%
21 4.30% 1.37% 0.35% 0.50% 0.60% 1.05% 0.68%
22 4.30% 1.37% 0.35% 0.50% 0.60% 1.10% 0.71%
23 4.30% 1.37% 0.35% 0.50% 0.60% 1.15% 0.73%
24 4.30% 1.37% 0.35% 0.50% 0.60% 1.20% 0.76%

关于python - 使用 Python 的组合方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51071223/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com