如何基于三列从数据帧中提取数据?

2022-09-28 20:01:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从基于三列的数据帧中洗牌并提取重复的列

我尝试将三列转换成一个字典,存储它们的索引并比较高度。第4行不是唯一的

df['C']=df[["Color1","Color2","Color3"]].stack().apply(tuple)
df = df.duplicated(subset=["Color1","Color2","Color3"], keep=False)


     Height    Color1    Color2    Color3
0    Short      NaN       Blue      Red
1    High       Red       Blue      NaN
2    Medium     Blue       Red      NaN 
3    Short       NaN       NaN      Blue
4    Short       NaN       Red      Blue
5    High        NaN       NaN      NaN

代码的输出应为:

     Height    Color1    Color2    Color3
0    Short      NaN       Blue      Red
1    High       Red       Blue      NaN
2    Medium     Blue       Red      NaN

Tags: 数据df字典高度stackbluerednanmediumshortheighthighcolor1color3color2

热门问题