【问题标题】:Error in CSV file in PythonPython 中的 CSV 文件出错
【发布时间】:2017-06-01 06:09:02
【问题描述】:

当我运行我的 Python 代码时,我得到了这个错误:

df = pd.DataFrame(desm)
scaler = StandardScaler()
scaler.fit(df)


ValueError                                Traceback (most recent call last)
<ipython-input-32-266a989a8af0> in <module>()
      1 scaler = StandardScaler()
----> 2 scaler.fit(df)

C:\Users\VILLAFAÑE\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py in fit(self, X, y)
    555         # Reset internal state before fitting
    556         self._reset()
--> 557         return self.partial_fit(X, y)
    558 
    559     def partial_fit(self, X, y=None):

C:\Users\VILLAFAÑE\Anaconda3\lib\site-packages\sklearn\preprocessing\data.py in partial_fit(self, X, y)
    578         X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
    579                         ensure_2d=False, warn_on_dtype=True,
--> 580                         estimator=self, dtype=FLOAT_DTYPES)
    581 
    582         if X.ndim == 1:

C:\Users\VILLAFAÑE\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    371                                       force_all_finite)
    372     else:
--> 373         array = np.array(array, dtype=dtype, order=order, copy=copy)
    374 
    375         if ensure_2d:

ValueError: could not convert string to float: 'PMP'

我的 Python 代码是:

import pandas as pd
desm = pd.read_csv("G:/BASES DE DATOS/desm4.csv")

我知道这是 csv 格式的问题,但我不知道如何解决。 请帮忙! 这是csv文件的链接,了解更多信息

https://drive.google.com/file/d/0B7tO-O0lx79FSnR0cVA3MDhrTG8/view?usp=sharing

【问题讨论】:

    标签: python csv jupyter


    【解决方案1】:

    您正在尝试缩放第一列是字符串而不是浮点数的数据集。

    您需要按如下方式读取数据框:

    import pandas as pd
    from sklearn.preprocessing import StandardScaler
    df = pd.read_csv('desm4.csv',index_col=0)
    scaler = StandardScaler()
    scaler.fit(df)
    

    试试上面的代码,如果你遇到任何问题,请告诉我。上面的代码获取站点列并将其用作索引(每行的行 ID),标准缩放器不会应用于索引,因此您不会收到错误。

    另外,你不必这样做

    df = pd.DataFrame(desm)
    

    pd.read_csv 读取一个 csv 并返回一个数据帧

    【讨论】:

      【解决方案2】:

      出现您的问题是因为默认的 read_csv() 方法使用 Index as None,这意味着它假定的第一列作为可以在其文档 here 中读取的索引。

      index_col : int or sequence or False, default None
      
      Column to use as the row labels of the DataFrame.
      If a sequence is given, a MultiIndex is used. 
      If you have a malformed file with delimiters at the end of each line,
      you might consider index_col=False to force pandas to _not_ use the first 
      column as the index (row names)
      

      因此,请尝试使用此

      import pandas as pd
      desm = pd.read_csv("G:/BASES DE DATOS/desm4.csv",index_col = False)
      

      我希望它有效。如果有任何问题,请告诉我。 快乐编码。干杯!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        • 1970-01-01
        • 2021-05-22
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多