vivace
vivace

Reputation: 57

'int' object has no attribute 'replace' error in python3.x

I don't get why this error occurs. Coz from my point of view the three columns 'WWBO','IBO','DBO' has exact same structure but when I apply 'replace' only WWBO works. Does it have sth with fillna? Need your help!

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

Error is::: AttributeError: 'int' object has no attribute 'replace'

Upvotes: 0

Views: 5831

Answers (2)

0xFK
0xFK

Reputation: 2728

it is Pandas bugs auto casting '0' values to int, to solutions for this either eliminate the 0 value or cast the columns to string as below

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

Upvotes: 4

Ankit Kumar Namdeo
Ankit Kumar Namdeo

Reputation: 1464

clearly one or more of these fields(market['WWBO'], market['IBO'], market['DBO']) have integer values and you are trying to perform string operation i.e. replace over it that's it is throwing error that

AttributeError: 'int' object has no attribute 'replace'

could you first print those values and see what are they or if you have many then its better to perform type check first like

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

let me know if this works for you or not

Upvotes: 1

Related Questions