gpt4 book ai didi

python - 在数据流python中连接两个csv数据

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:40 25 4
gpt4 key购买 nike

我有两个 csv 文件 - 我想在数据流中完全加入

我读取为 PCollection 的两个 csv 文件

csv1

A |乙| C | d | E

csv2

A |乙| C | F | G

我需要根据键 A,B 连接两个 P 集合,并得到如下所示的结果 p 集合

A |乙| C | d |电子| F | G

试用1

{'left': P_collection_1, 'right': P_collection_2}
| ' Combine' >> beam.CoGroupByKey()
| ' ExtractValues' >> beam.Values()

这基本上就像 sql 中的完全连接

最佳答案

我相信你确实可以使用CoGroupBykey:

应用 Apache Beam Programming guide在您的案例中的电话和电子邮件示例中,您可以尝试向 CoGroupByKey 提供“C、D、E”的 PCollection(以“A、B”为键)和“F、G”的 PCollection(也以“A”为键), B 的。

为了更清楚一点,每个 PCollection 中的元素必须是元组,第一个元素是“A,B”键,第二个元素是“C,D,E”或“F,G”值:

PColl1 = PCollection(
('2,4', '1,2,5'),
('1,10', '4,4,9'),
...) # this is the PCollection of CDE's

PColl2 = PCollection(
('2,4', '30,3'),
('20,1', '2,1'),
...) # this is the PCollection of FG's

(PCollection符号只是为了说明)

然后我们会申请:

join = {'CDE': PColl1, 'FG': Pcoll2} | beam.CoGroupByKey()

根据编程指南,结果应该是:

PCollection(
('2,4', {
'CDE': ['1,2,5'],
'FG': ['30,3']
}
),
('1,10', {
'CDE': ['4,4,9']
}
),
('20,1', {
'FG': ['2,1']
}
),
...)

如果 A 和 B 在同一个文件中多次取值 2,4,这应该不是问题,我们在 CDE 或 FG 中应该有多个值。

关于python - 在数据流python中连接两个csv数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53480599/

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