Reputation: 41
I have a pandas dataframe with one of the columns called artist. I would like to append a new row only if the new artist name is not in this column.
I tried but with no success:
if (all_data != name.all(axis = 0)):
all_data = all_data.append({'artist':str(name), 'netWorth':str(worth.strip())}, ignore_index = True)
This is all the code I have:
def get_webpage(i, url):
URL = url+str(i)
response = requests.get(URL)
return bs4.BeautifulSoup(response.text, 'html.parser')
COLUMNS = ['artist', 'netWorth']
all_data = pd.DataFrame(columns = COLUMNS)
def scrape(soup):
artists = soup.find_all('article', class_ = 'thumb-wrap')
for ar in artists:
name = ar.h3.a.text
worth = ar.div.find('div', class_='bc-networth').text
global all_data
if (all_data['artist'] != name).any():
all_data = all_data.append({'artist':str(name), 'netWorth':str(worth.strip())}, ignore_index = True)
i = 1
url = 'http://www.therichest.com/celebnetworth-category/celeb/singer/page/'
while (i<=14):
soup = get_webpage(i, url)
i = i+1
data = scrape(soup)
i = 1
url = 'http://www.therichest.com/celebnetworth-category/celeb/musician/page/'
while (i<=7):
soup = get_webpage(i, url)
i = i+1
data = scrape(soup)
Upvotes: 1
Views: 795
Reputation: 863156
I believe need check one column artist
only:
if (all_data['artist'] != str(name)).all():
Sample:
all_data = pd.DataFrame({'netWorth':[5,3],
'artist':list('ab')})
print (all_data)
netWorth artist
0 5 a
1 3 b
name = 'a'
b = 10
if (all_data['artist'] != str(name)).all():
all_data = all_data.append({'artist':str(name), 'netWorth':b }, ignore_index = True)
print (all_data)
netWorth artist
0 5 a
1 3 b
name = 'd'
b = 10
if (all_data['artist'] != name).all():
all_data = all_data.append({'artist':str(name), 'netWorth':b }, ignore_index = True)
print (all_data)
netWorth artist
0 5 a
1 3 b
2 10 d
Upvotes: 1