繁体   English   中英

重命名熊猫字符串向量中的选定单元格

[英]Rename Selected Cells Within Pandas String Vector

我只是想重命名熊猫数据框中“位置”列中的某些单元格。

数据框的开始看起来像这样:

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

我最好的猜测是:

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

但我收到以下错误消息:

ValueError: Length of values does not match length of index

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

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

暂无
暂无

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

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