【发布时间】:2019-05-10 22:05:57
【问题描述】:
我是 Python 和 Pandas 的新手,我正在尝试用特定值替换数组中的所有空值。
每次我运行它时,更新的值都不会保留。
我发现 Pandas 在迭代行时不保存更改...那么我该如何保存更改?
这是我的代码
animal_kinds = set(df.AnimalKind) # this gives categories used below in the "ak" like dog, cat, bird
new_color_dog = 'polka dots'
new_color_cat = 'plaid'
new_color_bird = 'stripes'
for ak in animal_kinds:
ak_colors = ak['colors']
ak_with_no_color = animals[(df["Kind"] == ak ) & (df["Color"] == "" ) ]
result_count = len(ak_with_no_color)
if result_count:
ak_with_no_color.at["Color"] = new_color_ak #sets new color based on kind of animal (ak)
print(str(ak) 'color is changed to ' + str(new_color_ak))
【问题讨论】:
标签: python python-3.x pandas dataframe for-loop