0

I have a dataframe that looks like this.

+----+-------+
| ID | Value |
+----+-------+
|  1 | 23    |
|  1 | NA    |
|  1 | NA    |
|  1 | NA    |
|  2 | 24    |
|  2 | NA    |
|  2 | NA    |
+----+-------+

For each ID value in a group, I either have one value or NA. I wanted to apply this non NA value to the NA values in that specific group as below.

Desired Output :

+----+-------+
| ID | Value |
+----+-------+
|  1 | 23    |
|  1 | 23    |
|  1 | 23    |
|  1 | 23    |
|  2 | 24    |
|  2 | 24    |
|  2 | 24    |
+----+-------+

How can we achieve this in pandas?

2 Answers2

2

Check with

df.Value.fillna(df.groupby('ID').Value.transform('first'), inplace=True)
BENY
  • 317,841
  • 20
  • 164
  • 234
1

say your dataframe variable is df

Then,

df.value.fillna(method='ffill')

Should do the trick.

Documentation for your reference :

Forward Fill Documentation Reference

High-Octane
  • 1,104
  • 5
  • 19