Pandas - FillNa with another column

0 votes

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 in Python by ana1504.k
• 7,890 points
979 views

1 answer to this question.

0 votes

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
answered Jul 5 by SDeb
• 13,190 points

Related Questions In Python

0 votes
1 answer

How to create empty pandas dataframe only with column names?

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

answered Apr 5 in Python by Hari
552 views
+2 votes
2 answers

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

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

answered Aug 13, 2018 in Python by bug_seeker
• 15,360 points
7,728 views
+1 vote
1 answer

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

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

answered Aug 8, 2018 in Python by Priyaj
• 56,940 points
38 views
0 votes
1 answer

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

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

answered Aug 30, 2018 in Python by Priyaj
• 56,940 points
2,104 views
+1 vote
2 answers

how can i count the items in a list?

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

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 348 views
0 votes
1 answer

Python Pandas: selecting element in array column

pa.loc[row] selects the row with label row. pa.loc[row, ...READ MORE

answered May 13 in Python by SDeb
• 13,190 points
70 views
0 votes
1 answer

Splitting a column with multiple values in python

Try using the following: df['id'] = df.index+1 df.set_index('id').col_name.str.split(',', expand ...READ MORE

answered Jul 9 in Python by SDeb
• 13,190 points
31 views