Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Loop multiplying values by itself

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading