0

I am trying to read different tabs from an excel file. For each tab I a want to perform a linear regression that returns a function (1st order polynom) I want to store the function under the name of tab or the data frame

import pandas as pd
import numpy as np


dfs = pd.read_excel(r'Prod.xlsx', sheet_name=None)
for df in dfs:
     func = np.poly1d(np.polyfit(dfs[df].P, dfs[df].Q, 1))
     def df(x):
         return func(x)

I would like to have a function with an individual name for regression each of the sheets. I tried also

for df in dfs:
     df = np.poly1d(np.polyfit(dfs[df].P, dfs[df].Q, 1))
     

but the problem is that df is being overwritten!

Glycerine
  • 7,157
  • 4
  • 39
  • 65
samhar
  • 13
  • 7
  • 1
    I'm really confused about what you're trying to achieve - could you give an example of how you'd like to be able to use these functions afterwards? – SpoonMeiser Sep 28 '21 at 12:41
  • 1
    Sounds like the "variable variables" problem: https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables – ForceBru Sep 28 '21 at 12:43
  • I assume you want to have a dictionary `polynomials`, where the key is the column name inside the dataframe, while the value is the corresponding polynomial? – André Sep 28 '21 at 12:44

1 Answers1

3

I want to store the function under the name of tab or the data frame

I think you just want a dictionary of functions:

polynomial_functions = {}
tabs = pd.read_excel(r'Prod.xlsx', sheet_name=None)
for tab in tabs:
     func = np.poly1d(np.polyfit(dfs[df].P, dfs[df].Q, 1))
     polynomial_functions[tab] = func

Then you can call later with

polynomial_functions["tab_name"]()

Note that I've renamed your df to tab to make it clearer what a dataframe represents.

2e0byo
  • 5,305
  • 1
  • 6
  • 26