0

I have a large excel data with one sheet (.xlsx) such as the following:

Time Flow
1 10
2 20
... ...

I need to extract every column as a variable to python and I need to use the corresponding heading of the column in excel as the variable name in python (e.g. in Time = [1,2,...]).

So far I get the values as following:

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

directory = os.getcwd()
path = os.path.realpath("Data.xlsx")
data = pd.read_excel(path)

It automatically excludes the headings from the numbers. I can get the headings as objects as follows:

datanames = Series (data.columns,dtype='object')

which gives:

In [42]:datanames
Out[42]: 
0             Time
1             Flow
.
.
.

but I'm not able to allocate the headings to each column.

I appreciate your support. Regards EMO

JNevill
  • 46,980
  • 4
  • 38
  • 63
EMO
  • 1
  • 1
  • You might check out one of these posts. This is probably a duplicate at some level: https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable-given-its-name-in-a-string or https://stackoverflow.com/questions/47496415/how-can-i-select-a-variable-by-string-name – JonSG Jan 19 '23 at 14:39
  • If I'm reading this correctly, you are asking how to turn each column in a DataFrame into list and assigning that list to a variable named after the column. Is that right? – Steven Rumbalski Jan 19 '23 at 15:13
  • @StevenRumbalski, yes exactly... The data is initially in .xlsx format. Each column has a heading. I need to read each column from the excel file as a list. Moreover I need to name each list the same as its corresponding heading in the excel file. I gave an example above. – EMO Jan 19 '23 at 15:19
  • If you're willing to assign to global scope you can do `globals().update(data.to_dict(orient='list'))`. Better would be to leave the data in a dictionary as in `d = data.to_dict(orient='list')`. Even better would be to do nothing and just use the DataFrame. – Steven Rumbalski Jan 19 '23 at 16:12

0 Answers0