我只是在了解熊猫的情况,不能解决一个问题。我有一份纽约州的县名单。如果这个县是5个行政区之一,我想把这个县的名字改成纽约,否则我就不管了。下面给出了这个想法,但并不正确。你知道吗
编辑-因此,如果县列中的前几行县是奥尔巴尼,阿勒格尼,布朗克斯前的变化,他们将奥尔巴尼,阿勒格尼,纽约后的变化
# clean up county names
# 5 boroughs must be combined to New York City
# eliminate the word county
nyCounties = ["Kings", "Queens", "Bronx", "Richmond", "New York"]
nypopdf['County'] = ['New York' for nypopdf['County'] in nyCounties else
nypopdf['County']]
小模型:
因此,这是使用pd.Series.where
~df['county'].isin(c)
选择不在列表中的行c
(开始处的~
是'not'操作),第二个参数是要替换的值(当条件为False时)。你知道吗为了符合您的示例:
或者
完整示例:
相关问题 更多 >
编程相关推荐