我是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”函数,但我无法理解如何在这种情况下使用它。
答案 0 :(得分:0)
您可以相应地格式化输出:
-f file
输出:
n = 2.83415e+07
print(f'{n:f}')
print(f'{n:e}')
请参阅string format mini language
熊猫的工作方式相同:
28341500.000000
2.834150e+07
输出:
import pandas as pd
df = pd.DataFrame ( [{"tata": 2.325568e9}])
# print with default float settings
print (df)
pd.options.display.float_format = '{:,.4f}'.format # set other global format
# print with changed float settings
print(df)
# really convert the type:
df["tata"] = df["tata"].astype(int)
# print with default int settings
print(df)
还有其他方法可以进行格式化 - 请参阅How to display pandas DataFrame of floats using a format string for columns?
答案 1 :(得分:0)
我有同样的问题。使用
df['Tata'].map(int)
答案 2 :(得分:0)
这对我有用,值得一试:
df['col_name'] = df['col_name'].astype('int64')
答案 3 :(得分:0)
pd.set_option('display.float_format', '{:.2f}'.format)
df = pd.DataFrame({'Traded Value':[67867869890077.96,78973434444543.44],
'Deals':[789797, 789878]})
print(df)
交易价值 | 优惠 | |
---|---|---|
0 | 67867869890077.96 | 789797 |
1 | 78973434444543.44 | 789878 |