Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling cell based on existing cells

Tags:

python

pandas

I have data in following format:

8A564   nan         json
8A928   nan         json
8A563   nan         json
8A564   10616280    json
8A563   10616222    json
8A564   nan         json
8B1BB   10982483    json
8A564   10616280    json

I would like to fill data in second column to match row that has same first column and non null value in second. So I would get following:

8A564   10616280    json
8A928   nan         json
8A563   10616222    json
8A564   10616280    json
8A563   10616222    json
8A564   10616280    json
8B1BB   10982483    json
8A564   10616280    json

How can it be achieved?

like image 773
MarkAlanFrank Avatar asked Jul 17 '19 14:07

MarkAlanFrank


People also ask

How do you make a cell autofill based on another cell?

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).

How do you autofill a cell range with the same data?

Method #1: Ctrl + D Click in the cell with the data and, keeping the left mouse button pressed, drag to select the rest of the cells in the row or column that you would like autofilled. Release the mouse button. Press Ctrl + D (the Ctrl key is held while the D key is pressed) and the cells are filled.

How to automatically fill a table with corresponding values of cells?

You need to copy this formula to all other cells in the second row for columns C, D, E on the «Register» sheet. As a result, the table is automatically filled with the corresponding values of cells.

How to autofill cells based on another cell?

Auto Fill the Blank Cells Based on Another Cell 4. Using the IF function to Autofill Cell 5. Using the VLOOKUP function to Autofill Cell Download this practice sheet to practice while you are reading this article. In this section, we will show you five of the most effective ways to auto-fill cells. 1. Autofill to End-of-Data in Excel

How to make autocomplete cells in Excel?

How to make the autocomplete cells in Excel: In the «Register» sheet you need to enter in the cell A2 any registration number from the column E on the «Database» sheet. Now, in the cell B2 in the «Register» sheet, you need to enter the cell auto-complete formula in Excel: You need to copy this formula to all other cells in ...

How to auto fill formulas in Microsoft Excel 365?

Select cell “D4” and apply the “CONCATENATE” function. Press “Enter” to apply the function. And the auto-filled result is here based on those two columns. Now simply click and hold your mouse on the lower right corner of the formula cell and drag it down to apply the same formula.


Video Answer


1 Answers

groupby and bfill

Keep in mind the the 0 in groupby(0) refers to the column named 0. If your column has a different name, use that.

df.groupby(0).bfill()

       0         1     2
0  8A564  10616280  json
1  8A928       NaN  json
2  8A563  10616222  json
3  8A564  10616280  json
4  8A563  10616222  json
5  8A564  10616280  json
6  8B1BB  10982483  json
7  8A564  10616280  json

If the ordering of what is null doesn't lend itself to back filling, you can get the first non-null value.

df[1] = df.groupby(0)[1].transform('first')
df

       0         1     2
0  8A564  10616280  json
1  8A928       NaN  json
2  8A563  10616222  json
3  8A564  10616280  json
4  8A563  10616222  json
5  8A564  10616280  json
6  8B1BB  10982483  json
7  8A564  10616280  json
like image 181
piRSquared Avatar answered Sep 21 '22 21:09

piRSquared