简体   繁体   English

使用 Python Pandas 合并.dat 文件

[英]Merging .dat files using Python Pandas

I'm attempting to merge two.dat files so I can scan through them both for a project.我正在尝试合并两个 .dat 文件,以便我可以扫描它们以找到一个项目。 I have tried searching it up, and all I can find is how to merge csv files.我试过搜索它,我只能找到如何合并 csv 文件。 How do I merge these two files using Pandas?如何使用 Pandas 合并这两个文件? If its necessary or is just plain easier to convert them, how do I do so?如果有必要或者更容易转换它们,我该怎么做? I am using Jupyterlabs, Python 3.8, and am quite new to both.我正在使用 Jupyterlabs,Python 3.8,对两者都很陌生。

Merging should go pretty much the same as csv:合并应该 go 与 csv 几乎相同:

import pandas as pd


df1 = pd.read_csv("fileA.dat")
df2 = pd.read_csv("fileB.dat")

df = pd.concat([df1, df2])

df.to_csv('df.dat')

this does however, partly depend on the conventions used in the dat file.但是,这确实部分取决于 dat 文件中使用的约定。 You might have to use a different parser to go through the files您可能必须通过文件对 go 使用不同的解析器

Try:尝试:

df_1 = pd.read_csv('file_1.dat', sep='\s\s+', engine='python') 
df_2 = pd.read_csv('file_2.dat', sep='\s\s+', engine='python')

df_3 = df_1.merge(df2, on = "common_column")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM