Python 熊猫:总金额显示列表
我在汇总和求和时遇到问题 文件中的示例:Python 熊猫:总金额显示列表,python,pandas,jupyter,Python,Pandas,Jupyter,我在汇总和求和时遇到问题 文件中的示例: 18820 Multiple choice / by Zambra, Alejandro, ZAMBRA B F 15.00 15.00 18821 Green girl / by Zambreno, Kate, ZAMBREN B F 15.00 15.00 18822 Milena, or, The most beautiful femur in the wo... ZEPEDA B F 19.00
18820 Multiple choice / by Zambra, Alejandro, ZAMBRA B F 15.00 15.00
18821 Green girl / by Zambreno, Kate, ZAMBREN B F 15.00 15.00
18822 Milena, or, The most beautiful femur in the wo... ZEPEDA B F 19.00 19.00
18823 Death notice : by Zhou, Haohui, ZHOU B F 27.00 27.00
18824 Billy Pintos war / by Zimmer, Michael, ZIMMER B F 26.00 26.00
我的代码:
print_itypes = df.loc[df['itype'].isin(['B','NB','CLUB',])]
print_stuff = print_itypes[pd.to_numeric(print_itypes['replacement_cost'], errors='coerce').notnull()]
print_stuff.groupby("itype").agg({"replacement_cost": np.sum})
输出:
B 30.0018.0023.0019.0018.0020.0020.0013.0028.002...
CLUB 5.0015.0015.0010.0010.0015.0017.0015.0010.008....
NB 17.9529.0029.0016.0016.0016.9515.0015.0015.002...
不求和,只列出清单。我必须在等式中的某个地方使用
来表示数值,否则,python会阻塞并抱怨非数值…等等。如果没有这个,我什么都做不到。似乎列replacement\u cost
是一个字符串,因此sum()
将值串联起来,而不是求和
尝试以下方法:
print_stuff['replacement_cost']=print_stuff['replacement_cost'].astype(float)
这将把列replacement\u cost
转换为浮点数,并发布groupby().sum()
将按预期工作。似乎列replacement\u cost
是一个字符串,因此sum()
将值串联起来而不是求和
尝试以下方法:
print_stuff['replacement_cost']=print_stuff['replacement_cost'].astype(float)
这将把列replacement\u cost
转换为float,并发布groupby().sum()
将按预期工作。print\u stuff['replacement\u cost']=print\u stuff['replacement\u cost'].astype(float)
,然后执行代码..@anky 91有效!如果你想把它设置为一个答案,我会进行绿色检查。谢谢@anky_91绿色检查!再次感谢,这正是我所需要的。print_stuff['replacement_cost']=print_stuff['replacement_cost'].astype(float)
,然后执行代码..@anky_91成功了!如果你想把它设置为一个答案,我会进行绿色检查。谢谢@anky_91绿色检查!再次感谢,这正是我所需要的。