我是 python 的初学者,我试图从数据集中获取具有最高 idmb 评级和最高总计的行,但我的 gross_total 值不是整数。我怎样才能把它转换成整数?以及如何获得用于执行统计功能的特定值。
import pandas as pd
dataset=pd.read_excel('movies.xls')
name=dataset['Title']
idmb=dataset['IMDB Score']
networth=dataset['Gross Earnings']
test_df=pd.DataFrame({'movie':name,
'rating':idmb,
'gross_total':networth})
nds=test_df.dropna(axis=0,how='any')
a=nds['gross_total'].astype(int)
highest_rating =nds.loc[nds['rating'].idxmax()]
highiest_networth=nds.loc[ nds['gross_total'].idxmax()]
print(highest_rating)
print(highiest_networth)
我得到这个输出
gross_total 2.83415e+07
movie The Shawshank Redemption
rating 9.3
Name: 742, dtype: object
我已经搜索并开始了解“pd.to_numeric”和“astype”函数,但我无法理解如何在这种情况下使用它。
原文由 Muhammad Ahmed 发布,翻译遵循 CC BY-SA 4.0 许可协议
这对我有用,值得一试: