I am trying analyze some data from wiki. I am trying to append some values from one column of a data frame to a blank list based on condition from another column from the same data frame. length of new list should come to 34 but it is coming to 1632. Below is the code.
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
import requests
page_wiki=requests.get("https://en.wikipedia.org/wiki/List_of_helicopter_prison_escapes#Actual_attempts")
def data_from_url(url,tag_name,class_name):
response = requests.get(url)
if response.status_code != 200:
print('HTTP 404: Page not found')
else:
soup = BeautifulSoup(response.text,'html.parser')
html_output = soup.find(tag_name,{'class':class_name})
output = pd.read_html(str(html_output))
return output[0]
escape_data=data_from_url("https://en.wikipedia.org/wiki/List_of_helicopter_prison_escapes","table","wikitable")
escape_data[['Date', 'Year']] = escape_data['Date'].str.split(',', expand=True)
escape_data = escape_data[['Date', 'Year', 'Prison name', 'Country', 'Succeeded','Escapee(s)','Details']]
Years_Succeed=[]
for year in escape_data['Year']:
for i in escape_data['Succeeded']:
if i!='Yes':
continue
else:
Years_Succeed.append(year)
>Solution :
You should add Year and Succeeded on the same row
for year, i in zip(escape_data['Year'], escape_data['Succeeded']):
if i != 'Yes':
continue
else:
Years_Succeed.append(year)
Or with list comprehension
[year for year, i in zip(escape_data['Year'], escape_data['Succeeded']) if i == 'Yes']
But the most convenient is Pandas way
escape_data[escape_data['Succeeded'] == 'Yes']['Year'].to_list()
# Or
escape_data.loc[escape_data.index[escape_data['Succeeded'].eq('Yes')], 'Year'].to_list()