【问题标题】:'int' object has no attribute 'replace' error in python3.x'int' 对象在 python3.x 中没有属性 'replace' 错误
【发布时间】:2019-05-24 09:24:18
【问题描述】:

我不明白为什么会发生此错误。因为从我的角度来看,“WWBO”、“IBO”、“DBO”三列具有完全相同的结构,但是当我应用“替换”时,只有 WWBO 有效。它和fillna有什么关系吗? 需要你的帮助!

import requests
from bs4 import BeautifulSoup as bs

#Read url
URL = "https://www.the-numbers.com/box-office-records/worldwide/all-            movies/cumulative/released-in-2019"
data = requests.get(URL).text

#parse url
soup = bs(data, "html.parser")

#find the tables you want
table = soup.findAll("table")[1:]

#read it into pandas
df = pd.read_html(str(table))

#concat both the tables
df = pd.concat([df[0],df[1]])
df = df.rename(columns={'Rank':'Rank',
             'Movie':'Title',
             'Worldwide Box Office':'WWBO',
             'Domestic Box Office':'DBO',
             'International Box Office':'IBO',
             'DomesticShare':'Share'})

#drop columns
market = df.drop(columns=['Rank','Share'])
market = market.fillna(0)

#replace $ -> '' 
market['WWBO'] = market['WWBO'].map(lambda s: s.replace('$',''))
market['IBO'] = market['IBO'].map(lambda s: s.replace('$',''))
market['DBO'] = market['DBO'].map(lambda s: s.replace('$',''))

market

错误是::: AttributeError: 'int' 对象没有属性 'replace'

【问题讨论】:

    标签: pandas web-scraping


    【解决方案1】:

    这是 Pandas 错误自动将 '0' 值转换为 int 的问题,解决方案是消除 0 值或将列转换为字符串,如下所示

    import pandas as pd
    import requests
    from bs4 import BeautifulSoup as bs
    
    #Read url
    URL = "https://www.the-numbers.com/box-office-records/worldwide/all-movies/cumulative/released-in-2019"
    data = requests.get(URL).text
    
    #parse url
    soup = bs(data, "html.parser")
    
    #find the tables you want
    table = soup.findAll("table")[1:]
    
    #read it into pandas
    df = pd.read_html(str(table))
    
    #concat both the tables
    df = pd.concat([df[0],df[1]])
    df = df.rename(columns={'Rank':'Rank',
                 'Movie':'Title',
                 'Worldwide Box Office':'WWBO',
                 'Domestic Box Office':'DBO',
                 'International Box Office':'IBO',
                 'DomesticShare':'Share'})
    
    #drop columns
    market = df.drop(columns=['Rank','Share'])
    market = market.fillna(0)
    
    #replace $ -> '' 
    market['WWBO'] = market['WWBO'].map(lambda s: s.replace('$',''))
    market['IBO']=market['IBO'].astype(str)
    market['IBO'] = market['IBO'].map(lambda s: s.replace('$',''))
    market['DBO']=market['DBO'].astype(str)
    market['DBO'] = market['DBO'].map(lambda s: s.replace('$',''))
    
    >> market[['WWBO','IBO','DBO']]
           WWBO            IBO          DBO
    0   2,622,240,021  1,842,814,023  779,425,998
    1   1,121,905,659    696,535,598  425,370,061
    2     692,163,684    692,163,684            0
    3     518,883,574    358,491,094  160,392,480
    4     402,976,036    317,265,826   85,710,210
    5     358,234,705    220,034,625  138,200,080
    6     342,904,508    231,276,537  111,627,971
    7     326,150,303    326,150,303            0
    8     293,766,097    192,548,368  101,217,729
    9     255,832,826    255,832,826            0
    10    253,940,650     79,203,380  174,737,270
    11    245,303,505    134,268,500  111,035,005
    12    190,454,964     84,648,456  105,806,508
    13    155,313,390     98,312,634   57,000,756
    

    【讨论】:

    • 天哪,真烦人,非常感谢
    【解决方案2】:

    显然这些字段中的一个或多个(ma​​rket['WWBO'], market['IBO'], market['DBO'])具有整数值,并且您正在尝试执行字符串操作即替换它,这就是它引发的错误

    AttributeError: 'int' 对象没有属性 'replace'

    你能不能先打印这些值,看看它们是什么,或者如果你有很多,那么最好先执行类型检查

    if market['WWBO'].dtype == object:
        market['WWBO'].map(lambda s: s.replace('$',''))
    else:
        pass
    

    让我知道这是否适合你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 2014-03-30
      • 2020-05-25
      • 1970-01-01
      相关资源
      最近更新 更多