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
CodePudding user response:
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]]