A.Rahman Mahmoud
A.Rahman Mahmoud

Reputation: 328

Pandas: a Loop across all Rows

I'm stuck on this and I really appreciate any help, I have this dataset

dataframe= pd.DataFrame(data={'col1': [True, True,True,False,True,False], 
                              'col2': ['a', 'b','c','d','e','f']})

I want to change every False value from col1 with it's equivalent from col2 so i tried this

for x, y in zip(dataframe['col1'], dataframe['col2']):
    if x == False:
        dataframe['col1']=dataframe['col1'].replace(x, y,regex=True)

I expected this

   col1 col2
0  True    a
1  True    b
2  True    c
3     d    d
4  True    e
5     f    f

but instead, I got this

   col1 col2
0  True    a
1  True    b
2  True    c
3     d    d
4  True    e
5     d    f

Upvotes: 2

Views: 64

Answers (2)

Scott Boston
Scott Boston

Reputation: 153460

Try using boolean indexing and assigning 'col2' to 'col1':

df.loc[~df['col1'], 'col1'] = df['col2']
df

Output:

   col1 col2
0  True    a
1  True    b
2  True    c
3     d    d
4  True    e
5     f    f

Upvotes: 2

Ben.T
Ben.T

Reputation: 29635

you don't need a loop and can use where to replace the False by nan that are then filled with the second column.

dataframe['col1'] = dataframe['col1'].where(dataframe['col1'], dataframe['col2'])
print(dataframe)
   col1 col2
0  True    a
1  True    b
2  True    c
3     d    d
4  True    e
5     f    f

or the same result with replace:

dataframe['col1'] = dataframe['col1'].replace(False, dataframe['col2'])

Upvotes: 2

Related Questions