简体   繁体   中英

Rename Selected Cells Within Pandas String Vector

I am simply trying to rename some of the cells within the 'location' column in a pandas dataframe.

The beginning of the dataframe looks like this:

Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25                   ASHEVILLE
Apr 25               ASHEVILLE N C
Apr 25               ASHEVILLE N C
Apr 25               ASHEVILLE N C

My best guess is:

postings.location = ["ASHEVILLE" for x in postings["location"] if "ASHEVILLE" in x]

but I get the following error message:

ValueError: Length of values does not match length of index

您可以将.loc与.str访问器一起使用,并contains

postings.loc[postings.location.str.contains('ASHEVILLE'),'location'] = 'ASHEVILLE'

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