【问题标题】:converting exponent or scientific number into integer in pandas python在pandas python中将指数或科学数转换为整数
【发布时间】:2018-09-24 23:19:30
【问题描述】:

我是 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”功能,但我不明白如何在这种情况下使用它。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    这对我有用,值得一试:

    df['col_name'] = df['col_name'].astype('int64') 
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。使用

      df['Tata'].map(int)
      

      【讨论】:

        【解决方案3】:

        你相应地格式化你的输出:

        n =  2.83415e+07
        
        print(f'{n:f}')
        print(f'{n:e}')
        

        输出:

        28341500.000000
        2.834150e+07
        

        string format mini language

        Pandas 也一样:

        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)
        

        归功于:unutbu's answer here

        输出:

                   tata
        0  2.325568e+09          # before format change
        
                        tata
        0 2.325.568.000,0000     # after format change
        
        
                 tata            # after int conversion
        0 -2147483648
        

        还有其他方法可以进行格式化 - 请参阅 How to display pandas DataFrame of floats using a format string for columns?

        【讨论】:

          【解决方案4】:
          pd.set_option('display.float_format', '{:.2f}'.format)
          df = pd.DataFrame({'Traded Value':[67867869890077.96,78973434444543.44],
                        'Deals':[789797, 789878]})
          print(df)
          
          Traded Value Deals
          0 67867869890077.96 789797
          1 78973434444543.44 789878

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-08-17
            • 2014-12-23
            • 1970-01-01
            • 2019-04-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多