0

I've been searching for honestly a couple weeks for an answer to this question and haven't yet found one - my search history is downright shameful at this point. That said, I'm quite new to Python (and programming in general), so it's certainly possible that I simply am not phrasing my question correctly and it's been answered before - I apologize if that's the case.

I'm attempting to create a list when a function is run that uses a parameter from the function in the created list's name, but I'm having no luck.

Sample of data used:

Station Date    Tmax    Tmin    Tavg    DewPoint    WetBulb Heat    CodeSum PrecipTotal ... Mist    Haze    Rain    Thunderstorm    Fog Patches Drizzle Smoke   Squall  NoLoggedCodes
0   1   5/1/2007    83  50  67  51  56  0   NO  0.0 ... 0   0   0   0   0   0   0   0   0   1
1   1   5/2/2007    59  42  51  42  47  14  BR  0.0 ... 1   0   0   0   0   0   0   0   0   0
2   1   5/3/2007    66  46  56  40  48  9   NO  0.0 ... 0   0   0   0   0   0   0   0   0   1
3   1   5/4/2007    66  49  58  41  50  7   RA  0.0 ... 0   0   1   0   0   0   0   0   0   0
4   1   5/5/2007    66  53  60  38  49  5   NO  0.0 ... 0   0   0   0   0   0   0   0   0   1

I know what's shown in the following is the wrong syntax, but hopefully it gives an idea of what I'm attempting. The function itself (outside of the added syntax errors) works fine for its purpose, but my goal is to have it also create a list of the new columns that were created and ideally follows the naming convention shown below. Any ideas how I can accomplish that?

def rollingfunction(days):
   """creates new rolling mean columns for each weather station for each item in list Keys"""
   WeatherCon{days}Day = [] #this line is incorrect, but I hope gives an idea of my goal
   for x in Keys:
      Station1[f'Last{days}DayAvg{x}'] Station1[x].rolling(days).mean()
      Station2[f'Last{days}DayAvg{x}'] Station2[x].rolling(days).mean()
      WeatherCon{days}Day.append([f'Last{days}DayAvg{x}']) #this line is also incorrect, but again context

#this is the list and list name that I want to create when the function is run with a call of 5. 
#This list is simply the names of the new columns created by the function so I can reference these columns later.

WeatherCon5Day = ['Last5DayAvgHaze', 'Last5DayAvgPrecipTotal', 'Last5DayAvgThunderstorm', 'Last5DayAvgFog', 'Last5DayAvgPatches', 'Last5DayAvgSmoke', 'Last5DayAvgSquall', 'Last5DayAvgNoLoggedCodes', 'Last5DayAvgTavg', 'Last5DayAvgAvgSpeed', 'Last5DayAvgDewPoint']

When the function is called for 5, it returns new individual columns for each weather pattern in Keys.

Presently, I'm using these new columns to perform a regression to mosquito growth

Ideally, my end goal is to be able to perform regressions on all weather values to mosquito population growth for every length of days up to around 15-20 and then use that to create a line graph showing coef and P-values over time to highlight when each value has the most (if any) effect. Obviously, calling rollingfunction() 15+ times and manually creating 15+ lists to use to get the needed regression values is silly, error prone, and just ugly. Additionally, I also recognize that what I'm attempting would create over 100 new columns and may also be quite silly.

I'd really appreciate any help y'all could give - even just pointing me in the right way. Thanks so much!

  • Here's a question that basically asks the same thing: https://stackoverflow.com/questions/47496415/how-can-i-select-a-variable-by-string-name/47496649?noredirect=1#comment128695707_47496649 . The short answer is, you don't really want to do that. Why not store those new variable names as keys in a dictionary instead? – alexis Aug 02 '22 at 00:56

1 Answers1

0

Take a look at these:

>>> locals()
>>> globals()
>>> dir()
>>> help(setattr)

On that last one, note that it accepts initial argument of a string. It will let you bind a new variable to your favorite namespace, perhaps a function's local namespace.


That said, what you really want here is to import pandas so you can take advantage of dataframes. They have lots of features which exactly address your needs.

J_H
  • 17,926
  • 4
  • 24
  • 44