Home > Software design >  Get a specific value from a header
Get a specific value from a header

Time:10-07

I am trying to get a specific value from my header and use it later. The header I am having is: Weekly data: 2019-01-01 to 2019-12-31.

I am trying to get only "2019-01-01 to 2019-12-31" from this header.

data_range=pd.read_excel("weekly data.xls")
data_range=date_range.columns.values
data_range=date_range.split(":")
print(data_range)

The data set looks like this:

Index Weekly data: 2019-01-01 to 2019-12-31
0 Site Name
1 Site ID

when I run the script above, I get the following error: AttributeError: 'DataFrame' object has no attribute 'split'

Thanks!

CodePudding user response:

the date_range.columns.values is an array you have to first isolate the array element to extract text. Here is functionnal code

import pandas as pd

date_range=pd.read_excel("weekly data.xls")
print(date_range.columns.values[1].split(':')[1].strip())
>> '2019-01-01 to 2019-12-31'
  • Related