Ecko
Ecko

Reputation: 115

Fill NA with another list pandas

Basically, what I'm trying to do here is to fill the missing values in a dataframe column name "Colors" with string names/words from a separate list named "lst".

Given code does it's job in adding to the targeted row while other string names (e.g. Green..etc) still remains, which is good.

However, the problem is the whole list is added to the row, which is obviously because it's not iterated over each element from the list.

Not sure how would I manipulate two different lengths of variables in this case. Nested loop would result in out of range error.

Data frame:

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

Desired output:

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

code:

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

Upvotes: 0

Views: 241

Answers (1)

BENY
BENY

Reputation: 323226

Try to assign

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

Upvotes: 2

Related Questions