如何在pandas数据框的所有列中获取唯一值

时间:2018-01-15 06:23:23

标签: python pandas

我想列出Pandas数据帧中所有列中的所有唯一值,并将它们存储在另一个数据帧中。我已经尝试了这个,但它的附加行明智,我希望列明智。我怎么做?

raw_data = {'student_name': ['Miller', 'Miller', 'Ali', 'Miller'], 
        'test_score': [76, 75,74,76]}
      df2 = pd.DataFrame(raw_data, columns = ['student_name', 'test_score'])


      newDF = pd.DataFrame() 

      for column in df2.columns[0:]:
          dat = df2[column].drop_duplicates()
          df3 = pd.DataFrame(dat)
          newDF = newDF.append(df3)

print(newDF)


Expected Output:
student_name  test_score
Ali          74
Miller       75
             76

1 个答案:

答案 0 :(得分:4)

我认为你可以使用drop_duplicates

如果要检查某些列并保留第一行,如果是dupe:

newDF = df2.drop_duplicates('student_name')
print(newDF)
   student_name  test_score
0        Miller        76.0
1      Jacobson        88.0
2           Ali        84.0
3        Milner        67.0
4         Cooze        53.0
5         Jacon        96.0
6        Ryaner        64.0
7          Sone        91.0
8         Sloan        77.0
9         Piger        73.0
10        Riani        52.0

谢谢你,@cᴏʟᴅsᴘᴇᴇᴅ寻求另一种解决方案:

df2[~df2.student_name.duplicated()]

但是如果要将所有列一起检查为dupes,请保留第一行:

newDF = df2.drop_duplicates()
print(newDF)
   student_name  test_score
0        Miller        76.0
1      Jacobson        88.0
2           Ali        84.0
3        Milner        67.0
4         Cooze        53.0
5         Jacon        96.0
6        Ryaner        64.0
7          Sone        91.0
8         Sloan        77.0
9         Piger        73.0
10        Riani        52.0
11          Ali         NaN

按新样本编辑 - 删除重复项并按两列排序:

newDF = df2.drop_duplicates().sort_values(['student_name', 'test_score'])
print(newDF)
  student_name  test_score
2          Ali          74
1       Miller          75
0       Miller          76

EDIT1:如果要通过NaN s的第一列替换dupes:

newDF = df2.drop_duplicates().sort_values(['student_name', 'test_score'])
newDF['student_name'] = newDF['student_name'].mask(newDF['student_name'].duplicated())
print(newDF)
  student_name  test_score
2          Ali          74
1       Miller          75
0          NaN          76

EDIT2:更一般的解决方案:

newDF = df2.sort_values(df2.columns.tolist())
           .reset_index(drop=True)‌
           ​.apply(lambda x: x.drop_duplicates())