简体   繁体   中英

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:

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. 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. 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.

Use drop_duplicates and the str accessor instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM