python 如何将“price”列从字符串格式转换为数字格式?

uqjltbpv  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(165)
# Import required modules 

import requests
from bs4 import BeautifulSoup
import time
import pandas as pd

# Get data from webpage

mystocks = ['GOOG', 'META', 'MSFT', 'PLTR', 'TSLA', 'ZS', 'PYPL', 'SHOP', 'TTCF']

def getData(symbol):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; 
    Win64; x64) AppleWebKit/537.36 
    (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}
    url = f'https://finance.yahoo.com/quote/{symbol}'
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')    
    stock = {
    'symbol': symbol,
    'price': soup.find('div', {'class':'D(ib) 
    Mend(20px)'}).find_all('fin-streamer') 
    [0].text,
    }
    return stock   
        
def export_data(stockdata):
    df = pd.DataFrame(stockdata)
    df.to_excel("LETS GO2.xlsx")
    df = df.apply(pd.to_numeric)
    df.apply(pd.to_numeric, errors='ignore')  

if __name__ == '__main__':
    while True: 
        stockdata = []    
        for item in mystocks: 
            print(item)
            stockdata.append(getData(item))
        export_data(stockdata)
        time_wait = 10
        print(f'Waiting {time_wait} minutes...')
        time.sleep(time_wait * 60)

我需要将“price”列转换为数字格式,但解决方案df["A"] = pd.to_numeric(df["A"])不起作用...不再出现错误(这可能是我的代码的问题),但导出的Excel没有按要求返回数字数据类型。
感谢所有的帮助,谢谢!

jk9hmnmh

jk9hmnmh1#

像 这样 做 。

import pandas as pd  
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import scipy.optimize as sco
import datetime as dt
import math
from datetime import datetime, timedelta
from pandas_datareader import data as wb
from sklearn.cluster import KMeans
np.random.seed(777)

start = '2018-06-30'
end = '2020-06-30'
# N = 90
# start = datetime.now() - timedelta(days=N)
# end = dt.datetime.today()


tickers = ['AXP','AAPL','BA','CAT','CSCO','CVX','XOM','GS','HD','IBM','INTC','JNJ','KO','JPM','MCD','MMM','MRK','MSFT','NKE','PFE','PG','TRV','UNH','RTX','VZ','V','WBA','WMT','DIS','DOW']

thelen = len(tickers)

price_data = []
for ticker in tickers:
    prices = wb.DataReader(ticker, start = start, end = end, data_source='yahoo')[['Adj Close']]
    price_data.append(prices.assign(ticker=ticker)[['ticker', 'Adj Close']])

df = pd.concat(price_data)
df.dtypes
df.head()
df.shape

pd.set_option('display.max_columns', 500)

df = df.reset_index()
df = df.set_index('Date')
table = df.pivot(columns='ticker')
# By specifying col[1] in below list comprehension
# You can select the stock names under multi-level column
table.columns = [col[1] for col in table.columns]
table.head()

中 的 每 一 个
这 就是 你 的 下场

相关问题