首页 > 解决方案 > 使用时出现AttributeError;在 IBM Cloud Notebook 中插入代码 pandas DataFrame。该怎么办?

问题描述

当我在 watson studios 中使用“查找和添加数据”功能将 CSV 文件加载到我的笔记本中时,我遇到了问题。当我使用“插入代码”时,我得到了自动代码行,以将 CSV 文件转换为 pandas 数据框。

它似乎以某种方式加载数据,但它仍然给出以下错误。我没有对生成的默认代码进行任何更改。这里发生了什么?

import os, types
import pandas as pd
from botocore.client import Config
import ibm_boto3

def __iter__(self): return 0

if os.environ.get('RUNTIME_ENV_LOCATION_TYPE') == 'external':
    endpoint_xxx = 'https://s3.eu.cloud-object-storage.appdomain.cloud'
else:
    endpoint_xxx = 'https://s3.private.eu.cloud-object-storage.appdomain.cloud'

client_xxxxx = hidden

body = client_xxx.get_object(Bucket='xxx',Key='xxx.csv')['Body']

# add missing __iter__ method, so pandas accepts body as file-like object
if not hasattr(body, "__iter__"): body.__iter__ = types.MethodType( __iter__, body )

df_data_1 = pd.read_csv(body, sep=';')
df_data_1.head()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/core/frame.py in _repr_html_(self)
    732         GH3541, GH3573
    733         """
--> 734         width, height = console.get_console_size()
    735         max_columns = get_option("display.max_columns")
    736         nb_columns = len(self.columns)

/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/io/formats/format.py in to_html(self, buf, encoding, classes, notebook, border)
    980             Buffer to write to. If None, the output is returned as a string.
    981         encoding : str, default “utf-8”
--> 982             Set character encoding.
    983         classes : str or list-like
    984             classes to include in the `class` attribute of the opening

/opt/conda/envs/Python-3.7-main/lib/python3.7/site-packages/pandas/io/formats/html.py in __init__(self, formatter, classes, border, table_id, render_links)
     54         self.col_space = {
     55             column: f"{value}px" if isinstance(value, int) else value
---> 56             for column, value in self.fmt.col_space.items()
     57         }
     58 

AttributeError: 'NoneType' object has no attribute 'items'

            Naam      Type Provincie afkorting Bezoekadres straat  \
0    Aa en Hunze  Gemeente                  DR       Spiekersteeg   
1       Aalsmeer  Gemeente                  NH      Raadhuisplein   
2         Aalten  Gemeente                  GD          Hofstraat   
3  Achtkarspelen  Gemeente                  FR     Stationsstraat   
4   Alblasserdam  Gemeente                  ZH           Cortgene   

  Bezoekadres huisnummer  Bezoekadres toevoeging Bezoekadres postcode  \
0                      1                     NaN              9461 BH   
1                      1                     NaN              1431 EH   
2                      8                     NaN              7121 DM   
3                     18                     NaN              9285 NH   
4                      2                     NaN              2951 ED   

  Bezoekadres plaats  Bezoekadres latitude  Bezoekadres longitude  \
0             GIETEN                53.005                  6.760   
1           AALSMEER                52.267                  4.750   
2             AALTEN                51.926                  6.583   
3         BUITENPOST                53.255                  6.143   
4       ALBLASSERDAM                51.862                  4.658   

   Oppervlakte (km2)  Aantal inwoners  Inwoners per km2  
0                279            25390                90  
1                 34            31499               928  
2                 97            26962               277  
3                104            27935               268  
4                 10            20014              2001  

标签: pythonpandasibm-cloudload-csv

解决方案


推荐阅读