Python Forum
Looping a function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Looping a function (/thread-18221.html)



Looping a function - wendysling - May-09-2019

Hello,

I am trying to run a function that creates additional columns in a data frame in case the column is missing.

The columns are for months. If the month is missing, the function should create an empty column for the missing month(s).

Below is what I have. However, the function stops once the first missing month is found. I would like it to run all the way to the end.

Can you see what I am missing? Thanks!

def add_column_missing(dataframe):
   
        if 'February'not in list(dataframe): 
            return dataframe.assign(February='')      
      
        elif 'March'not in list(dataframe): 
            return dataframe.assign(March='')  
        
        elif 'April'not in list(dataframe): 
            return dataframe.assign(April='')  
        
        elif 'May'not in list(dataframe): 
            return dataframe.assign(May='')  
        
        elif 'June'not in list(dataframe): 
            return dataframe.assign(June='')    

        elif 'July'not in list(dataframe): 
            return dataframe.assign(July='') 
            
        elif 'August'not in list(dataframe): 
            return dataframe.assign(August='') 

        elif 'September'not in list(dataframe): 
            return dataframe.assign(September='') 
            
        elif 'October'not in list(dataframe): 
            return dataframe.assign(October='')
    
        elif 'November'not in list(dataframe): 
            return dataframe.assign(November='') 
           
        elif 'December'not in list(dataframe): 
            return dataframe.assign(December='') 
        
        else:
            print('done!')

n = 12

    while n > 0:
        n -= 1
    
    return add_column_missing(grossadds_tableau)



RE: Looping a function - Gribouillis - May-09-2019

The problem is that you have too many return statements. Try this function
import datetime as dt
months = [dt.date(2019, i, 1).strftime('%B') for i in range(2, 13)]

def add_column_missing(dataframe):
    L = list(dataframe)
    for m in (x for x in months if x not in L):
        dataframe.assign(**{m: ''})
    print('done!')
or perhaps even this
import datetime as dt
months = [dt.date(2019, i, 1).strftime('%B') for i in range(2, 13)]

def add_column_missing(dataframe):
    L = list(dataframe)
    D = {x: '' for x in months if x not in L}
    if D:
        dataframe.assign(**D)
    print('done!')



RE: Looping a function - perfringo - May-09-2019

It's not clear for me what rows starting from #39 suppose to do.

If function returns something, control returns to the function caller. This means that after hitting first return function is done.

If you change dataframe you don't need to return anything.

I observe that there is no January + there are ways to express this logic in more DRY (Don't Repeat Yourself) style:

from calendar import month_name
for month in month_name:
    if month not in list(dataframe):
        dataframe.assign(month='')



RE: Looping a function - wendysling - May-09-2019

Thank you. This is working. But the columns are not appending to the original dataframe, as I can't add dataframe2 = dataframe.assign(month='').

So how can I append the new columns to the original dataframe and create a new copy?

def add_column_missing(dataframe):
    
    from calendar import month_name
    for month in month_name:
        if month not in list(dataframe):
              dataframe.assign(month='')