所以我有一个包含大量重复项的数据框,但问题是它们像这样-
Color ID
1 False 123456
2 True 123456
3 False 0987
4 False 0987
5 True 0987
在此示例中,我想合并所有重复的ID并将所有内容都设置为True
,但前提是True
列中有color
。
这是我的预期输出
ID Color
0 987 True
1 123456 True
但是我想对每一列都进行此操作,而不仅仅是对一列(在这种情况下为Color
)。
any
out=df.groupby('ID',as_index=False).Color.any()
out
Out[55]:
ID Color
0 987 True
1 123456 True
from pyspark.sql import functions as f
df.groupBy('ID').agg(
f.max(f.col('Color')).alias('Color')
)
或替代:
from pyspark.sql import functions as f df.groupBy('ID').agg( f.max(f.when(f.col('Color'), lit(1)).otherwise(lit(0))).cast('boolean').alias('Color') )