Hello, I apologize for any errors in advance.
By using the pandas, the row that is below the [ was put by Pandas, is the index of the table, soon does not count, you can withdraw.
Now your NO2_Ibirapuera object is in the following organization,
NO2_Ibirapuera = Direct object, does not have direct access to the table.
NO2_Ibirapuera [0] = And your table.
NO2_Ibirapuera [0] [0] = your first column.
NO2_Ibirapuera 0 = your second column.
Now just use NO2_Ibirapuera 0 [x], to define the x argument, with the value you want.
Let's create a function with the conditions:
def condition(var):
if var == "--":
return "--"
if 0 <= var <= 40:
return "Boa"
if 41 <= var <= 80:
return "Moderada"
if 81 <= var <= 120:
return "Ruim"
if 121 <= var <= 200:
return "Muito Ruim"
return "Péssima"
Now pass the data through the function:
for x in range(len(NO2_Ibirapuera[0][0])):
try:
NO2_Ibirapuera[0][1][x] = condition(int(NO2_Ibirapuera[0][0][x]))
except ValueError:
pass
Why try and except? Because I have to transform the data of the table into integer (int), if it is a letter will appear an error, so with the exception this item will be ignored, going to the next one.
Finalcode:
import requests
import pandas as pd
from bs4 import BeautifulSoup
url_dados='http://sistemasinter.cetesb.sp.gov.br/Ar/php/ar_dados_horarios_resultado.php'
r = requests.get(url_dados)
payload = {'texData':'27/02/2018',
'selEst':'5/Ibirapuera'}
response = requests.post(url_dados, data=payload)
with open('dados_ibira.html', 'wb') as f:
f.write(response.text.encode("utf8"))
soup = BeautifulSoup(response.content, 'html.parser')
table = soup.find_all('table')[6]
NO2_Ibirapuera = pd.read_html(str(table).replace(",", "."))
def condition(var):
if var == "--":
return "--"
if 0 <= var <= 40:
return "Boa"
if 41 <= var <= 80:
return "Moderada"
if 81 <= var <= 120:
return "Ruim"
if 121 <= var <= 200:
return "Muito Ruim"
return "Péssima"
for x in range(len(NO2_Ibirapuera[0][0])):
try:
NO2_Ibirapuera[0][1][x] = condition(int(NO2_Ibirapuera[0][0][x]))
except ValueError:
pass
print(NO2_Ibirapuera)
# Com isso o if/else é para ser aplicado nessa coluna com as seguintes condições:
#
# 0 - 40 = Boa
# 41 - 80 = Moderada
# 81 - 120 = Ruim
# 121 - 200 = Muito Ruim
# > 200 = Péssima
Vlw, hugs. Any questions, just ask.