简体   繁体   English

使用 Pandas (Python) 打开 .dat 文件

[英]Open .dat files using pandas (Python)

I just started learning Python and using pandas for data analysis and I would like to know what the right way of opening a .dat file is and if it would be better to convert .dat into .csv and use this file extension instead?我刚开始学习 Python 并使用 Pandas 进行数据分析,我想知道打开 .dat 文件的正确方法是什么,以及将 .dat 转换为 .csv 并使用此文件扩展名是否更好?

I tried to open the file by simply typing我试图通过简单地输入来打开文件

df_topex = open('datasets/TOPEX.dat', 'r')
print(df_topex)

and I got the following:我得到了以下信息:

<_io.TextIOWrapper name='datasets/TOPEX.dat' mode='r' encoding='UTF-8'>

When trying:尝试时:

df_topex = pd.read_csv('datasets/TOPEX.dat')
df_topex

the first row of data is considered as a header.第一行数据被视为标题。 In this particular data file, there are no headers so I would like this to be avoided.在这个特定的数据文件中,没有标题,所以我希望避免这种情况。 Is there a simple way of saying that this particular file has no headers or should I create them?有没有一种简单的方法可以说这个特定文件没有标题,或者我应该创建它们? If so, how?如果是这样,如何?

Just set header=None只需设置header=None

df_topex = pd.read_csv('datasets/TOPEX.dat', header=None)
df_topex

My experience is that pd.read_csv don't work when trying to import .dat-files so you could also consider using:我的经验是pd.read_csv在尝试导入 .dat 文件时不起作用,因此您也可以考虑使用:

topex = np.fromfile('datasets/TOPEX.dat')

And then coonvert it to Dataframe:然后将其转换为 Dataframe:

df_topex = pd.DataFrame(data=x)

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

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