Joehat
Joehat

Reputation: 1129

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?

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?

Upvotes: 2

Views: 16715

Answers (2)

bjornsing
bjornsing

Reputation: 332

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)

Upvotes: 2

Avenger789
Avenger789

Reputation: 402

Just set header=None

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

Upvotes: 3

Related Questions