pandas 使用read_html阅读分号分隔的数据

btxsgosb  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(125)

我想使用pandas.read_html()函数读取winequality-white.csv数据。
下面是我的代码:

import pandas as pd

wine = pd.DataFrame(
    pd.read_html(
        "https://github.com/shrikant-temburwar/Wine-Quality-Dataset/blob/master/winequality-white.csv",
        thousands=";",
        header=0,
    )[0]
)

......但结果是:

Unnamed: 0 "fixed acidity";"volatile acidity";"citric acid";"residual sugar";"chlorides";"free sulfur dioxide";"total sulfur dioxide";"density";"pH";"sulphates";"alcohol";"quality"
    0         NaN   7;0.27;0.36;20.7;0.045;45;170;1.001;3;0.45;8.8;6                                                                                                                       
    1         NaN  6.3;0.3;0.34;1.6;0.049;14;132;0.994;3.3;0.49;9...                                                                                                                       
    2         NaN  8.1;0.28;0.4;6.9;0.05;30;97;0.9951;3.26;0.44;1...                                                                                                                       
    3         NaN  7.2;0.23;0.32;8.5;0.058;47;186;0.9956;3.19;0.4...                                                                                                                       
    4         NaN  7.2;0.23;0.32;8.5;0.058;47;186;0.9956;3.19;0.4...

当然我可以选择raw,然后使用read_csv,但在html阅读的情况下,我如何修复它?

aij0ehis

aij0ehis1#

好的,这里有一个使用pd.read_html的选项:

import pandas as pd

wine = pd.read_html(
    "https://github.com/shrikant-temburwar/Wine-Quality-Dataset/blob/master/winequality-white.csv",
    header=0
)[0]

wine.drop('Unnamed: 0', axis=1, inplace=True)
headers = wine.columns[0].replace('"', '').split(';')
wine.columns = ['data']
wine[headers] = wine.data.str.split(';', expand=True)
wine.drop('data', axis=1, inplace=True)
wine.head()

上面的代码将导致:

>>> wine.head()
  fixed acidity volatile acidity citric acid residual sugar chlorides free sulfur dioxide total sulfur dioxide density    pH sulphates alcohol quality
0             7             0.27        0.36           20.7     0.045                  45                  170   1.001     3      0.45     8.8       6
1           6.3              0.3        0.34            1.6     0.049                  14                  132   0.994   3.3      0.49     9.5       6
2           8.1             0.28         0.4            6.9      0.05                  30                   97  0.9951  3.26      0.44    10.1       6
3           7.2             0.23        0.32            8.5     0.058                  47                  186  0.9956  3.19       0.4     9.9       6
4           7.2             0.23        0.32            8.5     0.058                  47                  186  0.9956  3.19       0.4     9.9       6
>>>

但我绝不会用下面的代码片段的简单性来交换上面的代码:

import pandas as pd

wine = pd.read_csv(
    'https://raw.githubusercontent.com/shrikant-temburwar/Wine-Quality-Dataset/master/winequality-white.csv',
    header=0,
    sep=';'
)
cl25kdpy

cl25kdpy2#

你可能会更好地使用github的rawdatacontent地址来消除由于不同html接口而引起的问题。
以下是您可以执行操作

import pandas as pd
import requests
import io
url = "https://raw.githubusercontent.com/shrikant-temburwar/Wine-Quality-Dataset/master/winequality-white.csv"
r = requests.get(url)
obj = io.BytesIO(r.content)
wine = pd.read_csv(obj, delimiter=";")
wine.head()

相关问题