distinct_values = df.col_name.unique().compute()
但是如果我不知道列的名称怎么办?
最佳答案
我认为你需要:
df = pd.DataFrame({"colA":['a', 'b', 'b', 'd', 'e'], "colB":[1,2,1,2,1]})
unique_dict = {}
# df.columns will give you list of columns in dataframe
for col in df.columns:
unique_dict[col] = list(df[col].unique())
输出:
{'colA': ['a', 'b', 'd', 'e'], 'colB': [1, 2]}
关于python - pandas 中多列的唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815986/