Following dataframe consists of two empty columns (Sales and Income), that should be updated with scraped data corresponding to the ticker column:
df = pd.DataFrame({
'No.': {0: '1', 1: '2', 2: '3', 3: '4', 4: '5'},
'Ticker': {0: 'AAPL', 1: 'MSFT', 2: 'TSLA', 3: 'FB', 4: 'BRK-B'},
'Sales': {0: '', 1: '', 2: '', 3: '', 4: ''},
'Income': {0: '', 1: '', 2: '', 3: '', 4: ''},
'Company': {0: 'Apple Inc.', 1: 'Microsoft Corporation', 2: 'Tesla, Inc.', 3: 'Meta Platforms, Inc.', 4: 'Berkshire Hathaway Inc.'},
'Sector': {0: 'Technology', 1: 'Technology', 2: 'Consumer Cyclical', 3: 'Communication Services', 4: 'Financial'}
})
With additional code below I am able to pull in the Sales and Income information from the snapshot table, but only for one ticker and I do not know how to update the dataframe:
Additional Code
ticker = (df.iloc[0,1])
#---------- Pulling the data from chosen stock ticker ----------#
url = ('https://finviz.com/quote.ashx?t=' ticker.upper())
req = requests.get(url,headers=headers)
table = pd.read_html(req.text, attrs = {"class":"snapshot-table2"} )
df = table[0]
print(f'{df[0][3]}: {df[1][3]}: {df[0][2]}: {df[1][2]}')
Expected result:
| No. | Ticker | Sales | Income | Company | Sector |
|---|---|---|---|---|---|
| 1 | AAPL | 365.82B | 94.68B | Apple Inc. | Technology |
| 2 | MSFT | 176.25B | 67.88B | Microsoft Corporation | Technology |
| 3 | TSLA | 46.85B | 3.47B | Tesla, Inc. | Consumer Cyclical |
| 4 | FB | 112.33B | 40.30B | Meta Platforms, Inc. | Communication Services |
| 5 | BRK-B | 268.68B | - | Berkshire Hathaway Inc. | Financial |
CodePudding user response:
How to achieve?
Put your additional code into a def and use zip() in combination with map() to update your columns:
df['Sales'],df['Income'] = zip(*df['Ticker'].map(lambda x: get_ticker(x)))
Example
def get_ticker(ticker):
#---------- Pulling the data from chosen stock ticker ----------#
url = ('https://finviz.com/quote.ashx?t=' ticker.upper())
req = requests.get(url,headers=headers)
table = pd.read_html(req.text, attrs = {"class":"snapshot-table2"} )
df = table[0]
return(df[1][3],df[1][2])
df['Sales'],df['Income'] = zip(*df['Ticker'].map(lambda x: get_ticker(x)))
Output
| No. | Ticker | Sales | Income | Company | Sector |
|---|---|---|---|---|---|
| 1 | AAPL | 365.82B | 94.68B | Apple Inc. | Technology |
| 2 | MSFT | 176.25B | 67.88B | Microsoft Corporation | Technology |
| 3 | TSLA | 46.85B | 3.47B | Tesla, Inc. | Consumer Cyclical |
| 4 | FB | 112.33B | 40.30B | Meta Platforms, Inc. | Communication Services |
| 5 | BRK-B | 268.68B | - | Berkshire Hathaway Inc. | Financial |
