I'm trying to transfer strings into integers from the plans_data
:
import pandas as pd
from bs4 import BeautifulSoup
import requests
plans_data = pd.DataFrame(columns = ['Country', 'Currency', 'Mobile', 'Basic', 'Standard', 'Premium'])
for index, row in countries_list.iterrows():
country = row['ID']
url = f'https://help.netflix.com/en/node/24926/{country}'
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find("table", class_="c-table")
try:
plan_country = pd.read_html(results.prettify())[0] #creates a list(!) of dataframe objects
plan_country = plan_country.rename(columns = {'Unnamed: 0':'Currency'})
plan_country = pd.DataFrame(plan_country.iloc[0,:]).transpose()
plans_data = pd.concat([plans_data, plan_country], ignore_index=True)
except AttributeError:
country_name = row['Name']
print(f'No data found for {country_name}.')
plans_data.loc[index, 'Country'] = row['Name']
plans_data
Firstly, I atempted to transfer using function float
:
# 1. Here we import pandas
import pandas as pd
# 2. Here we import numpy
import numpy as np
ans_2_1_ = float(plans_data['Basic', 'Standard', 'Premium'])
However, I always get the NameError:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_15368/3072127414.py in <module>
3 # 2. Here we import numpy
4 import numpy as np
----> 5 ans_2_1_ = float(plans_data['Basic', 'Standard', 'Premium'])
NameError: name 'plans_data' is not defined
How can I solve this problem?
If my code is not appropriate for my task "transfer strings into integers", can you advise me on how to convert?
CodePudding user response:
The error indicates that the second piece of code does not know what plans_data
is, so first make sure they plans_data
is defined where you do that, ie in the same file or the same Jupyter notebook
second problem is that plans_data['Basic', 'Standard', 'Premium']
is not valid syntax
Thirdly, and probably is your real question, how to convert the values in those columns to floats.
Elements in columns 'Basic', 'Standard', 'Premium' are strings in currency format eg '£ 5.99'
. You can convert them to floats as such (you need to do it for each column):
ans_2_1_ = plans_data['Basic'].str[1:].astype(float)
... # same for Standard and Premium