1

I have a data frame with two columns in which one column header is "5" (5 is the number of time-series rows), the other is the "Value" column like this:

5   Value

2014-01-01 00:00:00 2.80

2014-01-01 00:15:00 2.97

2014-01-01 00:30:00 2.74

2014-01-01 00:45:00 2.54

2014-01-01 01:00:00 2.28

I would like to add some text rows above the two columns header when saving the data frame to *.dat (or *CSV) file format like this:

$Column1=Time (days)

$Column2=Temperature (°C)

5   Value

2014-01-01 00:00:00 2.80

2014-01-01 00:15:00 2.97

2014-01-01 00:30:00 2.74

2014-01-01 00:45:00 2.54

2014-01-01 01:00:00 2.28

Thank you in advance.

1 Answer 1

2

Write your csv with to_csv and reload your data with read_csv like this:

metadata = """\
$Column1=Time (days)
$Column2=Temperature (°C)
"""

# Write csv
with open('data.csv', 'w') as fp:
    fp.write(metadata)
    df.to_csv(fp, sep='\t', index=False)

# Read csv
df1 = pd.read_csv('data.csv', comment='$', sep='\t', parse_dates=[0])
>>> %cat 'data.csv'
$Column1=Time (days)
$Column2=Temperature (°C)
5   Value
2014-01-01 00:00:00 2.8
2014-01-01 00:15:00 2.97
2014-01-01 00:30:00 2.74
2014-01-01 00:45:00 2.54
2014-01-01 01:00:00 2.28


>>> df1
                    5  Value
0 2014-01-01 00:00:00   2.80
1 2014-01-01 00:15:00   2.97
2 2014-01-01 00:30:00   2.74
3 2014-01-01 00:45:00   2.54
4 2014-01-01 01:00:00   2.28
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.