我有一个带有用户ID和设备型号值的数据框
每个用户的用户ID是唯一的,但是多次登录有重复项,并且没有缺失值 设备型号缺失值
我需要为具有相同用户ID的用户解决相同的设备型号。像这样:
Device Model User ID
0 iPhone 11 1590761773215-7300441
1 iPhone XR 1590761549649-1399336
2 NAN 1590761549649-1399336
3 iPhone 7 1589030810407-6386535
4 NAN 1590761773215-7300441
Device Model User ID
0 iPhone 11 1590761773215-7300441
1 iPhone XR 1590761549649-1399336
2 iPhone XR 1590761549649-1399336
3 iPhone 7 1589030810407-6386535
4 iPhone 11 1590761773215-7300441
有人知道如何解决这个问题吗?我尝试使用join(如下),但是t work and I don
不知道有其他解决方案
join_df_device = pd.merge(device_model_df,device_type_nonull,on ='Device Model',how ='left')
join_df_device
答案 0 :(得分:1)
据我了解
使用df.groupby
将所有相似的User ID
分组并使用df.fillna
df['Device_Model'] = df.groupby('User_ID')['Device_Model'].ffill().bfill()
Device_Model User_ID
0 iPhone 11 1590761773215-7300441
1 iPhone XR 1590761549649-1399336
2 iPhone XR 1590761549649-1399336
3 iPhone 7 1589030810407-6386535
4 iPhone 11 1590761773215-7300441
答案 1 :(得分:1)
尽管在这种情况下,Ch3steR's answer更容易,但这是一种尝试合并的解决方案:
df[['User ID']].merge(df.dropna().drop_duplicates(), on='User ID')
结果:
User ID Device Model
0 1590761773215-7300441 iPhone 11
1 1590761773215-7300441 iPhone 11
2 1590761549649-1399336 iPhone XR
3 1590761549649-1399336 iPhone XR
4 1589030810407-6386535 iPhone 7