Python09
Python09

Reputation: 115

Fill Blank Row in a specific column with value from another column using pandas

If I have the following pandas dataframe

      A     B
0     Cat   Dog
1     Mouse Mouse
2     Fish  "Blank"

How do I parse through column B, identify that there is a blank value and then if there is a blank value fill in the blank with "Fish", so the desired data frame is:

      A     B
0     Cat   Dog
1     Mouse Mouse
2     Fish  Fish

Upvotes: 3

Views: 6363

Answers (2)

Sajan
Sajan

Reputation: 1267

You could try this -

import numpy as np
df['B'] = np.where(df['B'] == 'Blank', df['A'], df['B'])

Upvotes: 6

rgralma
rgralma

Reputation: 145

  • If only want to check from 'Blank' elements in column B:

df[df['B'] == 'Blank'] = 'Fish'

  • Check to entire dataframe:

df[df=='Blank'] = 'Fish'

Upvotes: 0

Related Questions