Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Dataframe保存python for循环中更新的值_Python_Python 3.x_Pandas_Dataframe_For Loop - Fatal编程技术网

使用Dataframe保存python for循环中更新的值

使用Dataframe保存python for循环中更新的值,python,python-3.x,pandas,dataframe,for-loop,Python,Python 3.x,Pandas,Dataframe,For Loop,我是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'

我是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))
避免链式索引 这种操作称为链式索引,它是:

df[(df['kind'] == 'dog') & (df['colour'] == '')].at['colour'] = 'black'
相反,请计算并使用布尔掩码:

mask = (df['kind'] == 'dog') & (df['colour'] == '')
df.loc[mask, 'colour'] = 'black'
使用字典查找可变数量的变量 这种操作在Python中不起作用:

new_colour_dog = 'polka dots'
new_colour+'_dog'  # want 'polka dots', but will not work
改用字典:

new_colours = {'dog': 'polka dots', 'cat': 'plaid', 'bird': 'stripes'}
然后可以迭代字典的键值对:

for animal, new_colour in new_colours.items():
    mask = (df['kind'] == animal) & (df['colour'] == '')
    df.loc[mask, 'colour'] = new_colour
mask
返回一系列
False
值时,不需要测试/特殊情况实例