我正在通过pd.read_html从Google Finance获取表数据,然后通过df.to_excel()将该数据保存到Excel,如下所示:

    dfs = pd.read_html('https://www.google.com/finance?q=NASDAQ%3AGOOGL&fstype=ii&ei=9YBMWIiaLo29e83Rr9AM', flavor='html5lib')
    xlWriter = pd.ExcelWriter(output.xlsx, engine='xlsxwriter')

    for i, df in enumerate(dfs):
        df.to_excel(xlWriter, sheet_name='Sheet{}'.format(i))
    xlWriter.save()

但是,保存到Excel中的数字存储为文本,单元格角上有一个绿色小三角形。将此数据移到Excel时,如何将其存储为实际值而不是文本?

最佳答案

考虑将数值列转换为浮点数,因为pd.read_html将Web数据读取为字符串类型(即对象)。但在转换为float之前,需要将连字符替换为nan:

import pandas as pd
import numpy as np

dfs = pd.read_html('https://www.google.com/finance?q=NASDAQ%3AGOOGL' +
                   '&fstype=ii&ei=9YBMWIiaLo29e83Rr9AM', flavor='html5lib')
xlWriter = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
workbook = xlWriter.book

for i, df in enumerate(dfs):
    for col in df.columns[1:]:                  # UPDATE ONLY NUMERIC COLS
        df.loc[df[col] == '-', col] = np.nan    # REPLACE HYPHEN WITH NaNs
        df[col] = df[col].astype(float)         # CONVERT TO FLOAT

    df.to_excel(xlWriter, sheet_name='Sheet{}'.format(i))

xlWriter.save()

关于python - Python df.to_excel()将数字存储为excel中的文本。如何存储为Value?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41080999/

10-11 08:27