Tyler
Tyler

Reputation: 107

How to name pandas dataframe columns from a text file

I have two text files, one is data and one is the headers.

The data looks like this in dataframe: enter image description here And the headers text file looks like this:

`[TramDateDimension]
(
       [Date]
      ,[CalendarYear]
      ,[FinancialYear]
      ,[FinancialMonth]
      ,[CalendarMonth]
      ,[CalendarMonthSeq]
      ,[CalendarQuarter]
      ,[FinancialQuarter]
      ,[CalendarWeek]...)
`

How can I name the columns using this text file?

Upvotes: 1

Views: 338

Answers (1)

jezrael
jezrael

Reputation: 862771

First return all values between [] with regex:

import re
with open("headers.txt") as f:
        s = ''.join(f.readlines())
        headers = re.findall('\[(.*?)\]',s)

print (headers)
['TramDateDimension', 'Date', 'CalendarYear', 'FinancialYear', 
 'FinancialMonth', 'CalendarMonth', 'CalendarMonthSeq', 
 'CalendarQuarter', 'FinancialQuarter', 'CalendarWeek']

And then call read_csv with parameter names with header[1:] to remove the first value of the list of headers:

df = pd.read_csv('file.csv', names=headers[1:])

Upvotes: 2

Related Questions