Whereas you have already been able to interpret the . dat file for Dataframe and your goal is just to name the columns:
df
3.141592543 3.141592543 3.141592543 3.141592543
3.141592543 3.141592543 3.141592543 3.141592543
3.141592543 3.141592543 3.141592543 3.141592543
df.columns = ['col1', 'col2', 'col3', 'col4']
col1 col2 col3 col4
3.141592543 3.141592543 3.141592543 3.141592543
3.141592543 3.141592543 3.141592543 3.141592543
3.141592543 3.141592543 3.141592543 3.141592543
Whereas you haven’t read the file yet, so you still need to interpret . dat
import pandas as pd
from io import StringIO
dat = """3.141592543 3.141592543 3.141592543 3.141592543
3.141592543 3.141592543 3.141592543 3.141592543
3.141592543 3.141592543 3.141592543 3.141592543"""
df = pd.read_csv(StringIO(dat), sep="\s+", header=None)
df.columns = ['col1', 'col2', 'col3', 'col4']
print(df)
read_csv
will perform the reading of a file within your file system and will try to turn it into a Dataframe.
sep
is the separator (delimiter) between the columns of the file
\s+
is a regex that is searching for occurrences of space. You can read more about regex here
header=None
says that your file . dat has no header (which will be added later, in your case)
Thank you so much for the answers. Both solved my problem!!
– F. Oliveira