How do I fill the missing value in one column with the value of another column?

I read that looping through each row would be very bad practice and that it would be better to do everything in one go but I could not find out how to do it with the fillna method.

Data Before

```Day  Cat1  Cat2
1    cat   mouse
2    dog   elephant
3    cat   giraf
4    NaN   ant```

Data After

```Day  Cat1  Cat2
1    cat   mouse
2    dog   elephant
3    cat   giraf
4    ant   ant```

How do I resolve this?

Jul 5, 2019 in Python 86,059 views

## 3 answers to this question.

You can provide this column to fillna, it will use those values on matching indexes to fill:

```In [17]: df['Cat1'].fillna(df['Cat2'])
Out[17]:
0    cat
1    dog
2    cat
3    ant
Name: Cat1, dtype: object```
• 13,300 points

Assuming three columns of your data frame is `a``b` and `c`. This is what you want:

``````df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)
``````

Full code:

``````df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)``````
• 65,910 points

You could do

`df.Cat1 = np.where(df.Cat1.isnull(), df.Cat2, df.Cat1)`
• 10,520 points

## How to create empty pandas dataframe only with column names?

You can do it like this: df=pd.DataFrame(columns=["Name","Old","Ne ...READ MORE

## How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

+1 vote

## How to replace id with attribute corresponding to id of another table?

Use the following query statement and let ...READ MORE

## How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

## how do i change string to a list?

suppose you have a string with a ...READ MORE

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE