用另一个列表熊猫填充 NA

时间:2021-02-02 15:50:49

标签: python pandas dataframe

基本上,我在这里要做的是用名为“lst”的单独列表中的字符串名称/单词填充数据框列名称“Colors”中的缺失值。

给定代码的工作是添加到目标行,而其他字符串名称(例如 Green..etc)仍然保留,这很好。

然而,问题是整个列表都被添加到行中,这显然是因为它没有迭代列表中的每个元素。

不确定在这种情况下我将如何操作两个不同长度的变量。嵌套循环会导致超出范围的错误。

数据框:

Index   Colors
0       nan
1       Red
2       nan
3       Green
4       nan
5       nan
6       Brown

所需的输出:

Index   Colors
0       one
1       Red
2       two
3       Green
4       three
5       four
6       Brown

代码:

import pandas as pd
from pandas import np

df_train = pd.DataFrame({'Colors': [np.nan, 'Red', np.nan, 'Green', np.nan, np.nan, 'Brown']})

lst = ['one','two','three','four']

for row in df_train .loc[df_train .file_name.isnull(), 'file_name'].index:
    df_train .at[row, 'file_name'] = [i for i in lst]
output of code:

    Colors
0   ['one', 'two', 'three', 'four']
1   Red
2   ['one', 'two', 'three', 'four']
3   Green
4   ['one', 'two', 'three', 'four']
5   ['one', 'two', 'three', 'four']
6   Brown

1 个答案:

答案 0 :(得分:2)

尝试分配

df.loc[df.Colors.isnull(),'Colors'] = lst 
df
Out[296]: 
   Index Colors
0      0    one
1      1    Red
2      2    two
3      3  Green
4      4  three
5      5   four
6      6  Brown