我是Python的新手,我正在尝试解决两组不同的数据问题。我已经使用pandas.read_csv
创建了两个数据框s和cS
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%
我读到熊猫和/或itertools可以帮助我进行这种分析?困惑是在尝试为数据帧 c 中两个字段的每个组合从 s 创建所有记录时。
同样,任何有关文档和/或见解的帮助都将非常有用。谢谢
答案 0 :(得分:4)
使用带有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%