您如何只填充数据帧中不完全为空的组?
在下面的数据框中,仅应填充具有df.A=b
和df.A=c
的组。
df
A B
0 a NaN
1 a NaN
2 a NaN
3 a NaN
4 b 4.0
5 b NaN
6 b 6.0
7 b 6.0
8 c 7.0
9 c NaN
10 c NaN
在想类似的东西:
if set(df[df.A==(need help here)].B.values) == {np.nan}:
。
答案 0 :(得分:1)
我们可以做page_number <- 1:20
groupby
答案 1 :(得分:0)
获取不完全为空的索引,然后在这些索引上forwardfill
/ backwardfill
df = df.set_index("A")
#get index where entries in B are not completely full
ind = df.loc[df.groupby("A").B.transform(lambda x: x.eq(x))].index.unique()
df.loc[ind] = df.loc[ind].ffill().bfill()
print(df)
B
A
a NaN
a NaN
a NaN
a NaN
b 4.0
b 4.0
b 6.0
b 6.0
c 7.0
c 7.0
c 7.0