Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Fill up first row in partition from another column?

Tags:

I have the following Dataframe, and I want to fill the first empty cell of the Inventory column when groupby on the Product column with the value from the adjacent row of the Stock column.

   Year  Week Product  Stock  Inventory
0  2019    21       A     10        NaN
1  2019    22       A     10       34.0
2  2019    23       A     10        NaN
3  2019    24       A     10       28.0
4  2019    25       C     20        NaN
5  2019    26       C     20       39.0
6  2019    27       C     20        NaN
7  2019    28       B     35        NaN
8  2019    29       B     35        NaN
9  2019    30       B     35       94.0

Final output should look like this

   Year  Week Product  Stock  Inventory
0  2019    21       A     10       10.0
1  2019    22       A     10       34.0
2  2019    23       A     10        NaN
3  2019    24       A     10       28.0
4  2019    25       C     20       20.0
5  2019    26       C     20       39.0
6  2019    27       C     20        NaN
7  2019    28       B     35       35.0
8  2019    29       B     35        NaN
9  2019    30       B     35       94.0

The data

import pandas as pd
import numpy as np

data = {
    "Year": [2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019],
    "Week": [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
    "Product": ["A", "A", "A", "A", "C", "C", "C", "B", "B", "B"],
    "Stock": [10, 10, 10, 10, 20, 20, 20, 35, 35, 35],
    "Inventory": [np.NaN, 34, np.NaN, 28, np.NaN, 39, np.NaN, np.NaN, np.NaN, 94]
}

df = pd.DataFrame(data)

print(df)
like image 399
Shantanu Avatar asked Sep 11 '19 15:09

Shantanu


1 Answers

Here is one way using combine_first after drop_duplicates

df.Inventory=df.Inventory.combine_first(df.drop_duplicates(['Product']).Stock)
df
Out[193]: 
   Year  Week Product  Stock  Inventory
0  2019    21       A     10       10.0
1  2019    22       A     10       34.0
2  2019    23       A     10        NaN
3  2019    24       A     10       28.0
4  2019    25       C     20       20.0
5  2019    26       C     20       39.0
6  2019    27       C     20        NaN
7  2019    28       B     35       35.0
8  2019    29       B     35        NaN
9  2019    30       B     35       94.0
like image 193
BENY Avatar answered Sep 30 '22 06:09

BENY