将浮点数附加到空 pandas DataFrame
Appending floats to empty pandas DataFrame
我正在尝试构建一个递归函数,它将从特定数据帧中获取数据,对其进行一些数学运算,然后将该结果附加到新的数据帧中。我当前的代码如下所示
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)
我收到一条错误消息:
TypeError: cannot concatenate object of type '<class 'numpy.float64'>'; only Series and DataFrame objs are valid
div1 和 di_obp 是数据帧,['g'] 是 di_obp 数据帧中的一列。
我试过将变量 games 变成一个空列表和一个系列,但出现了不同的错误。我不确定接下来应该尝试什么。非常感谢任何帮助!!
这里是 di_obp 数据帧的头部,dii_obp 和 diii_obp 数据帧相同但值不同。
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
当前问题:
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)
这不是 return 错误,但我的数据框仍然是空的
附加到数据框是 generally not recommended。相反,您应该在列表中积累数据,然后从这些列表创建数据框:
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]]
我正在尝试构建一个递归函数,它将从特定数据帧中获取数据,对其进行一些数学运算,然后将该结果附加到新的数据帧中。我当前的代码如下所示
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)
我收到一条错误消息:
TypeError: cannot concatenate object of type '<class 'numpy.float64'>'; only Series and DataFrame objs are valid
div1 和 di_obp 是数据帧,['g'] 是 di_obp 数据帧中的一列。
我试过将变量 games 变成一个空列表和一个系列,但出现了不同的错误。我不确定接下来应该尝试什么。非常感谢任何帮助!!
这里是 di_obp 数据帧的头部,dii_obp 和 diii_obp 数据帧相同但值不同。
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
当前问题:
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)
这不是 return 错误,但我的数据框仍然是空的
附加到数据框是 generally not recommended。相反,您应该在列表中积累数据,然后从这些列表创建数据框:
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]]