我在“ nearest_beacon”列中具有以下重复值的数据框,但在“ vms_distance”列中具有不同的距离:
nearest_beacon vms_distance associated
2890231 0.421313 vms
2890231 0.215785 vms
2890231 0.104256 vms*
4548780 0.486456 vms
4548780 0.468065 vms
4548780 0.337609 vms
4548780 0.363601 vms
4548780 0.210566 vms
4548780 0.197327 vms*
4548780 0.285390 vms
4548780 0.216443 vms
1221421 0.441454 vms
1221421 0.337533 vms*
我想确定每个“ nearest_beacon”值中一行(*)的“ associated”列,并在“ vms_distance”列中设置最小值,将“ associated”设置为“ vms”,其余设置为“ no_vms”。
预期结果:
nearest_beacon vms_distance associated
2890231 0.421313 no_vms
2890231 0.215785 no_vms
2890231 0.104256 vms
4548780 0.486456 no_vms
4548780 0.468065 no_vms
4548780 0.337609 no_vms
4548780 0.363601 no_vms
4548780 0.210566 no_vms
4548780 0.197327 vms
4548780 0.285390 no_vms
4548780 0.216443 no_vms
1221421 0.441454 no_vms
1221421 0.337533 vms
答案 0 :(得分:0)
将groupby
与idxmin
一起使用,然后通过loc
分配回去
df.loc[df.groupby('nearest_beacon').vms_distance.idxmin(),'associated']='no vms'
答案 1 :(得分:0)
尝试一下
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
userIdentity.AddClaim(new Claim("Code",this.Code));
return userIdentity;
}
//My extended property
public string Code { get; set; }
}
答案 2 :(得分:0)
执行以下操作:
df['associated'] = 'non_vms'
df.loc[df.groupby('nearest_beacon')['vms_distance'].idxmin(), 'associated'] = 'vms'
df
输出:
nearest_beacon vms_distance associated
0 2890231 0.421313 non_vms
1 2890231 0.215785 non_vms
2 2890231 0.104256 vms
3 4548780 0.486456 non_vms
4 4548780 0.468065 non_vms
5 4548780 0.337609 non_vms
6 4548780 0.363601 non_vms
7 4548780 0.210566 non_vms
8 4548780 0.197327 vms
9 4548780 0.285390 non_vms
10 4548780 0.216443 non_vms
11 1221421 0.441454 non_vms
12 1221421 0.337533 vms