0

I am trying create dynamic table names by using variable from the for loop. I am using Python 2.7.

This is the function I intend to write:

where columns can be parsed from the external sources.

FUNCTION:

def cutoff(row):
    if (row['CourierCode']=='DP'):
        for TAT in range(5):
            row[eval("TAT + "+str(TAT))]=[row.taskcreateddate==row.ReturnPickupDate+
timedelta(days=TAT)]

RefundwTaskData.apply(lambda row : func(cutoff),axis = 1)
  • Can you add more information? What is `CombinedData`, `ratio`, `month`? `list`, `numpy array`? Please check [How to make good reproducible pandas examples](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – jezrael Aug 03 '16 at 10:39
  • Your function is syntactically incorrect - you can't bind a result to the addition on the left-hand side of the equals sign. And where's this `for` loop you mention? – holdenweb Aug 03 '16 at 10:43
  • this is function i am trying to write: def cutoff(row): if (row['CourierCode']=='DP'): for TAT in range(5): row[eval("TAT + "+str(TAT))]=[row.taskcreateddate==row.ReturnPickupDate+ timedelta(days=TAT)] RefundwTaskData.apply(lambda row : func(cutoff),axis = 1) – Prateek Gupta Aug 18 '16 at 13:04

1 Answers1

1

In your case, if you do want to accomplish it this way use:

exec("CombinedDataProjected%s = %s" % (month, 'pd.DataFrame(CombinedData)'))

But this is a very poor way of executing in python since Python has perfectly good concepts for representing a bunch of variables --- lists and dictionaries!. I personally would have done it by using a dictionary in the following manner (no need to create a separate function)

CombinedDataProjected = {}
months = [201501,201502,201503]  #Some list of months
for month in months:
    <Some routines for creating CombinedData>
    CombinedDataProjected[key] = CombinedData
Gaurav Dhama
  • 1,346
  • 8
  • 19