微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

循环合并 df 和 .txt 并附加匹配的输出

如何解决循环合并 df 和 .txt 并附加匹配的输出

我使用的是 python3我有一个包含 13,527 家公司(和 5 列)的 operation = detect_person(input_uri,output_uri) 数据框。我想将此公司列表合并到变量 nameinternat_country 上的 Contact_info.txt 40+GB 文件(+1.9 亿家公司和 29 列)。

我想要的输出一个数据框(我在下面的代码中称之为 name_country),其中包含 13,527 家公司列表(来自 mergeall,我的左 df),以及来自匹配的 29 列的合并nameinternat_country 的情况。 Contact_info.txt 将有 13,527 行和 34 列(来自 mergeall 的 5 列 + 来自 nameinternat_country 的 29 列原始)。不匹配的案例将显示缺失值。

问题来自 Contact_info.txt 有 +40GB(由于内存问题,我无法将其作为数据帧加载)。所以我首先需要对它进行分块,然后逐块进行合并。这是我的代码(注意:我将 Contact_info.txt 文件子集为其前 5,000 行只是为了在我的试验中更有效):

Contact_info.txt

mergeall = pd.DataFrame() #create df to store merges in chunk below ChunkSize = 1000 #num of rows per chunk for chunk in pd.read_csv('ORBIS financial/Contact info.txt',sep="\t",nrows=5000,chunksize=ChunkSize): chunk["name_country"]= chunk["NAME_INTERNAT"]+","+chunk["Country"] #create new (merging) column in txt files mergeall = pd.concat([mergeall,nameinternat_country.merge(chunk,how='left',on='name_country')]) 数据框给了我 67,635 行 x 34 列。是 mergeall 函数中的错误吗?

非常感谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。