如何检查特定 pandas 数据框列中的值是否唯一

3 python unique duplicates dataframe pandas

我需要检查 pandas 数据框列中是否存在多次特定值。这是基本代码;

for index, row in df_x.iterrows():
    try:
        if row[1] in df_y['b'].values:  

# if row[1] exists in df_y i want to know how many time is it repeated, or if it is unique or not

    except Exception as e:
        print('Error ', e)
Run Code Online (Sandbox Code Playgroud)

小智 7

您有一个名为 df 的 DataFrame 所以您想知道由“spec_col”命名的特定列包含唯一值

import pandas as pd

pd.Series(df["spec_col"]).is_unique
>>> True 
Run Code Online (Sandbox Code Playgroud)

  • 我想转换为“pd.Series”甚至没有必要,所以“df['spec_col'].is_unique”已经可以工作了? (2认同)