简体   繁体   English

为什么我得到 AttributeError: 'numpy.ndarray' object has no attribute 'replace' in python?

[英]Why do I get AttributeError: 'numpy.ndarray' object has no attribute 'replace' in python?

I am trying to standardize my data in my csv file:我正在尝试在我的 csv 文件中标准化我的数据:

import pandas as pd
my_city = pd.read_csv('sample4_addlink.csv')['City'].unique()

my_city
Out[70]: 
array(['Lancaster', 'Canton', 'Edison', ..., 'Upton', 'Irvington',
       'El Cerrito'], dtype=object)

Now as you see in my_city, there are some cities with space between names like El Cerrito.现在,正如您在 my_city 中看到的那样,有些城市的名称之间有空格,例如 El Cerrito。 I want to replace the space with an underscore我想用下划线替换空格

my_cities =my_city.replace(" ", "_")
Traceback (most recent call last):

  File "<ipython-input-71-53c4a0662dd7>", line 1, in <module>
    my_cities =my_city.replace(" ", "_")

AttributeError: 'numpy.ndarray' object has no attribute 'replace'

I get this error when I use the .replace function.使用 .replace 函数时出现此错误。 What is the best way to get around this?解决这个问题的最佳方法是什么?

It's because unique returns an ndarray or ExtensionArray object depending on the data type of the input series.这是因为unique根据输入系列的数据类型返回ndarrayExtensionArray对象。

Use drop_duplicates and the str accessor instead:改用drop_duplicatesstr访问器:

my_city = pd.read_csv('sample4_addlink.csv')['City'].drop_duplicates().str.replace(' ', '_')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Numpy和Matplotlib-AttributeError:“ numpy.ndarray”对象没有属性“ replace” - Numpy and Matplotlib - AttributeError: 'numpy.ndarray' object has no attribute 'replace' AttributeError:“numpy.ndarray”对象在 Python 中没有属性“index” - AttributeError: 'numpy.ndarray' object has no attribute 'index' in Python AttributeError:“ numpy.ndarray”对象没有属性“ A” - AttributeError: 'numpy.ndarray' object has no attribute 'A' AttributeError: 'numpy.ndarray' object 没有属性 'get' - AttributeError: 'numpy.ndarray' object has no attribute 'get' Python - AttributeError:'numpy.ndarray'对象没有属性'append' - Python - AttributeError: 'numpy.ndarray' object has no attribute 'append' Python - AttributeError:&#39;numpy.ndarray&#39;对象没有属性&#39;to&#39; - Python - AttributeError: 'numpy.ndarray' object has no attribute 'to' Python AttributeError: 'numpy.ndarray' object 没有属性 'append' - Python AttributeError: 'numpy.ndarray' object has no attribute 'append' AttributeError:'numpy.ndarray' object 没有属性'get_shape'? - AttributeError: 'numpy.ndarray' object has no attribute 'get_shape'? 为什么我得到 &#39;numpy.ndarray&#39; 对象没有属性 &#39;convert&#39;? - Why do I get 'numpy.ndarray' object has no attribute 'convert'? 为什么 python 抛出错误:AttributeError: &#39;numpy.ndarray&#39; object has no attribute &#39;append&#39;? - Why is python throwing error : AttributeError: 'numpy.ndarray' object has no attribute 'append'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM