[英]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.