Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill empty cells in column with value of other columns

Tags:

python

pandas

na

I have a HC list in which every entry should have an ID, but some entries do not have an ID. I would like to fill those empty cells by combining the the first name column and the last name column. How would I go about this? I tried googling for fillna and the like but couldn't get it to work. What I want is basically this:

If hc["ID"] == "": 
    hc["ID"] = hc["First Name"] + hc["Last Name"]
like image 684
alpenmilch411 Avatar asked Apr 29 '16 15:04

alpenmilch411


People also ask

How do I autofill across columns?

Simply do the following: Select the cell with the formula and the adjacent cells you want to fill. Click Home > Fill, and choose either Down, Right, Up, or Left. Keyboard shortcut: You can also press Ctrl+D to fill the formula down in a column, or Ctrl+R to fill the formula to the right in a row.

How do I automatically fill columns based on cell value?

Anyone who has used Excel for some time knows how to use the autofill feature to autofill an Excel cell based on another. You simply click and hold your mouse in the lower right corner of the cell, and drag it down to apply the formula in that cell to every cell beneath it (similar to copying formulas in Excel).


1 Answers

You can use loc and a boolean mask if NaN then:

hc.loc[hc["ID"].isnull(),'ID'] = hc["First Name"] + hc["Last Name"] 

otherwise for empty string:

hc.loc[hc["ID"] == '','ID'] = hc["First Name"] + hc["Last Name"]
like image 186
EdChum Avatar answered Oct 18 '22 09:10

EdChum