我有以下数据框,我想在Inventory列上的 groupby 用Product列的相邻行的值填充列的第一个空单元格Stock。
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
最终输出应如下所示
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
数据
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)
Qyouu
慕姐4208626
相关分类