2

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?

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?

1
  • 1
    I strongly suggest learning python first, then pandas. You'll get caught up in pitfalls by basic functionality of the language if you dive in head-first with pandas. Commented Apr 1, 2020 at 13:54

2 Answers 2

4

Just set header=None

df_topex = pd.read_csv('datasets/TOPEX.dat', header=None)
df_topex
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! Would this be the right approach when dealing with .dat files? In terms of good programming.
To be honest, the file extension does not matter and the actual format matters - be it a comma separated or a tab separated. Pandas allows a very quick insight into the data and I'd suggest using this method for comma and tab separated files. If you do find the answer correct, please up vote and mark it as answer.
Would you mind explaining to me why the built-in function open() didn't work? I am struggling to understand why from the documentation.
It worked. What you stopped at is just the file open object. You are yet to read the file. print(df_topex .read()) will show you the contents of the file. readline() will show you a line from the file
2

My experience is that pd.read_csv don't work when trying to import .dat-files so you could also consider using:

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

And then coonvert it to Dataframe:

df_topex = pd.DataFrame(data=x)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.