python pandas groupby 之后 根据多列求unique

R语言转成python. 求助
SingleVRef
python pandas groupby 之后 根据多列求unique
R 语言如下

SingleVRef<- unique(SingleVRef[,c("Parameter","Step","max","min")])

写成下面的感觉不对劲

        SingleVRef['max'] = SingleVRef.groupby(['Par','Step'])['Value'].transform(lambda x: x.max())

SingleVRef['min'] = SingleVRef.groupby(['Par','Step'])['Value'].transform(lambda x: x.min())# min=min(Value))

SingleVRef = SingleVRef[["Par","Step","max","min"]].groupby(['Par','Step']).apply(lambda x: list(np.unique(x)))

SingleVRef.columns=["Par","Step","refUL","refLL"]

python pandas groupby 之后 根据多列求unique


回答:

Python:

SingleVRef[["Par","Step","max","min"]].drop_duplicates(inplace=True)

# 或者

SingleVRef = SingleVRef[["Par","Step","max","min"]].drop_duplicates()

见:pandas.DataFrame.drop_duplicates,unique: Extract Unique Elements。

注意,nunique() 中的 n 表示计数。

以上是 python pandas groupby 之后 根据多列求unique 的全部内容, 来源链接: utcz.com/p/938159.html

回到顶部