【发布时间】:2023-01-18 18:02:53
【问题描述】:
这是我的数据框。
df = pd.DataFrame ( {'CNN': ['iphone 11 63 GB TMO','iphone 11 128 GB ATT','iphone 11 other carrier','iphone 12 256 GB TMO','iphone 12 64 GB TMO','iphone 12 other carrier'],
'Family Name':['iphone 11', 'iphone 11', 'iphone 11', 'iphone 12', 'iphone 12', 'iphone 12'],
'Storage': [63, 128,np.nan, 256,64, np.nan]})
Output:
CNN Family Name Storage
0 iphone 11 63 GB TMO iphone 11 63.0
1 iphone 11 128 GB ATT iphone 11 128.0
2 iphone 11 other carrier iphone 11 NaN
3 iphone 12 256 GB TMO iphone 12 256.0
4 iphone 12 64 GB TMO iphone 12 64.0
5 iphone 12 other carrier iphone 12 NaN
我想要实现的是找到 NA。标准是来自组(姓氏)的最小存储量。我试图按 fillna(min()) 分组,但它似乎没有用。
#Tried
df["Storage"] = df.groupby("Family Name").apply(lambda x: x.fillna(x.min()))
这是预期的最终输出。
Expected Output:
CNN Family Name Storage
0 iphone 11 63 GB TMO iphone 11 63.0
1 iphone 11 128 GB ATT iphone 11 128.0
2 iphone 11 other carrier iphone 11 63.0
3 iphone 12 256 GB TMO iphone 12 256.0
4 iphone 12 64 GB TMO iphone 12 64.0
5 iphone 12 other carrier iphone 12 64.0
【问题讨论】: