Python 3.x 使用nunique标记数据帧中的重复值,但出现错误
我试图用注释标记唯一值,但得到的是TypeError:字符串索引必须是整数 输入 钥匙 ab 卑诗省 df ab 输出 关键|评论 ab |检查一下 卑诗省| df| ab |检查一下 条件_2=lambda x:如果x[Key].nunique>=1,则检查它,否则为0 df[注释]=半最终的df.Key.applycondition\u 2 错误:Python 3.x 使用nunique标记数据帧中的重复值,但出现错误,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我试图用注释标记唯一值,但得到的是TypeError:字符串索引必须是整数 输入 钥匙 ab 卑诗省 df ab 输出 关键|评论 ab |检查一下 卑诗省| df| ab |检查一下 条件_2=lambda x:如果x[Key].nunique>=1,则检查它,否则为0 df[注释]=半最终的df.Key.applycondition\u 2 错误: TypeError Traceback (most recent call la
TypeError Traceback (most recent call last)
<ipython-input-175-dc8d1ac8148f> in <module>
----> 1 semi_final_df["Comments"]=semi_final_df.Key.apply(condition_2)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
3192 else:
3193 values = self.astype(object).values
-> 3194 mapped = lib.map_infer(values, f, convert=convert_dtype)
3195
3196 if len(mapped) and isinstance(mapped[0], Series):
pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()
<ipython-input-174-cf54900ff760> in <lambda>(x)
----> 1 condition_2= lambda x: " Check it" if x["Key"].nunique()>=1 else 0
TypeError: string indices must be integers```
使用with keep=False可屏蔽所有具有以下特性的复制:
我猜你错过了与apply的axis争论。看见
df["Comments"]= np.where(df.Key.duplicated(keep=False), "Check it", '')
print (df)
Key Comments
0 ab Check it
1 bc
2 df
3 ab Check it