I built the code below and am having issues of how to transpose the results. Effectively I am looking for the following result:
# Column headers: 'company name', 'Work/Life Balance', 'Salary/Benefits', 'Job Security/Advancement', 'Management', 'Culture'
# Row 1: 3M, 3.8, 3.9, 3.5, 3.6, 3.8
# Row 2: Google, . . .
Currently what happens is as follows:
# Column headers: 'Name', 'Rating', 'Category'
# Row 1: 3M, 3.8, Work/Life Balance
# Row 2: 3M, 3.9, Salary/Benefits
# and so on . . .
My code thus far:
import requests
import pandas as pd
from bs4 import BeautifulSoup
number = []
category = []
name = []
company = ['3M', 'Google']
for company_name in company:
try:
url = 'https://ca.indeed.com/cmp/'+company_name
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
rating = soup.find(class_='cmp-ReviewAndRatingsStory-rating')
rating = rating.find('tbody')
rows = rating.find_all('tr')
except:
pass
for row in rows:
number.append(str(row.find_all('td')[0].text))
category.append(str(row.find_all('td')[2].text))
name.append(company_name)
cols = {'Name':name,'Rating':number,'Category':category}
df = pd.DataFrame(cols)
print(df)
What the code produces:
Name Rating Category
0 3M 3.8 Work/Life Balance
1 3M 3.9 Salary/Benefits
2 3M 3.5 Job Security/Advancement
3 3M 3.6 Management
4 3M 3.8 Culture
5 Google 4.2 Work/Life Balance
6 Google 4.0 Salary/Benefits
7 Google 3.6 Job Security/Advancement
8 Google 3.9 Management
9 Google 4.2 Culture
10 Apple 3.8 Work/Life Balance
11 Apple 4.1 Salary/Benefits
12 Apple 3.7 Job Security/Advancement
13 Apple 3.7 Management
14 Apple 4.1 Culture
replicate result by using code below:
import pandas as pd
name = ['3M','3M','3M','3M','3M','Google','Google','Google','Google','Google','Apple','Apple','Apple','Apple','Apple']
number = ['3.8','3.9','3.5','3.6','3.8','4.2','4.0','3.6','3.9','4.2','3.8','4.1','3.7','3.7','4.1']
category = ['Work/Life Balance',' Salary/Benefits','Job Security/Advancement','Management','Culture','Work/Life Balance',' Salary/Benefits','Job Security/Advancement','Management','Culture','Work/Life Balance',' Salary/Benefits','Job Security/Advancement','Management','Culture']
cols = {'Name':name,'Rating':number,'Category':category}
df = pd.DataFrame(cols)
print(df)
IndentationError: unindent does not match any outer indentation levelon thecols =line.ca.indeed.comis blocked by my company firewall, so unless you provide code that creates the dataframe without scraping it from the Internet, I can't investigate further.