1

I am trying to build a recursive function that will take data from a specific dataframe, do a little math with it, and append that result to a new dataframe. My current code looks as follows

div1, div2, div3 = [pd.DataFrame(index = range(1), columns = ['g']) for i in range(3)]

# THIS IS NOT WORKING FOR SOME REASON
def stats(div, obp):
    loop = 1
    while loop <= 3:
        games = obp['g'].sum() / 2
        div = div.append(games)
        loop += 1
    if loop == 2:
        stats(div2, dii_obp, dii_hr)
    elif loop == 3:
        stats(div3, diii_obp, diii_hr)
    else:
        print('Invalid')
        
stats(div1, di_obp)

I get an error that reads:

TypeError: cannot concatenate object of type '<class 'numpy.float64'>'; only Series and DataFrame objs are valid

div1, and di_obp are dataframes and ['g'] is a column in the di_obp dataframe.

I have tried making the variable games into an empty list, and a series and got different errors. I am not sure what I should try next. Any help is much appreciated!!

here is the head of the di_obp dataframe, the dii_obp and the diii_obp dataframes are the same but with different values.

print(di_obp.head())
      rank  team     g      ab      h     bb   hbp    sf    sh   
608  213.0  None  56.0  1947.0  526.0  182.0  55.0  19.0  22.0 
609  214.0  None  36.0  1099.0  287.0  124.0  25.0  11.0  24.0  
610  215.0  None  35.0  1099.0  247.0  159.0  51.0  11.0  24.0 
611  216.0  None  36.0  1258.0  317.0  157.0  30.0  11.0   7.0 
612  217.0  None  38.0  1136.0  281.0  138.0  41.0  14.0  10.0

CURRENT PROBLEM:

div1, div2, div3 = [pd.DataFrame(index= range(1), columns = ['g']) for i in range(3)]

def stats(div, obp):
    loop = 1
    while loop <= 3:
        while loop <= 3:
            games = obp['g'].sum() / 2
            div[0] = div[0].append({'g': games}, ignore_index=True)
            loop += 1
        if loop == 2:
            stats([div2], dii_obp)
        elif loop == 3:
            stats([div3], diii_obp)
        else:
            print('Done')

stats([div1], di_obp)

this does not return an error, but my dataframes are still empty

Jensen_ray
  • 81
  • 2
  • 10

1 Answers1

1

Appending to a dataframe is generally not recommended. Instead, you should accumulate your data in lists and then create dataframes from those lists:

div1, div2, div3 = [[] for _ in range(3)]

def stats(div, obp):
    loop = 1
    while loop <= 3:
        while loop <= 3:
            games = obp['g'].sum() / 2
            div.append(games)
            loop += 1
        if loop == 2:
            stats(div2, dii_obp)
        elif loop == 3:
            stats(div3, diii_obp)
        else:
            print('Done')

stats(div1, di_obp)

div1_df, div2_df, div2_df = [pd.DataFrame({'g': div}) for div in [div1, div2, div3]]
  • When I change the div1, div2, and div3 variables from dataframes to lists, and run the code you suggested, I get a new error: – Jensen_ray Feb 08 '22 at 23:37
  • AttributeError: 'NoneType' object has no attribute 'append' – Jensen_ray Feb 08 '22 at 23:37
  • Why change them to lists? Keep all your dataframes just like they were. The point of this solution is only for when you use the dataframes in functions - you can't pass by reference in Python, so this is a workaround. –  Feb 08 '22 at 23:42
  • well I tried it because when I do it with a dataframe I get: – Jensen_ray Feb 08 '22 at 23:50
  • TypeError: Can only append a dict if ignore_index=True – Jensen_ray Feb 08 '22 at 23:50
  • Oh yes, I'm sorry, that's my fault. You're supposed to call `append` with `ignore_index=True`. Check the answer now. –  Feb 08 '22 at 23:52
  • and when I make it true, the dataframe is still totally empty – Jensen_ray Feb 08 '22 at 23:52
  • Will you please add the code you're working with to the question? I think if I can see it I can help you better ;) –  Feb 08 '22 at 23:54
  • Yeah I am sorry haha I am just excited to have some help, I appriciate it – Jensen_ray Feb 08 '22 at 23:55
  • No problem! It's fun :) –  Feb 08 '22 at 23:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241843/discussion-between-jensen-ray-and-richardec). – Jensen_ray Feb 08 '22 at 23:57