-1
import pandas as pd
df = pd.DataFrame({'col1': ['A', 'B', 'C'],
                       'col2': ['B', 'A', 'A']})
df

How would I SELECT DISTINCT col1, col2 from df?

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
williamscathy825
  • 185
  • 1
  • 1
  • 11
  • Mayowa Ayodele. This worked thanks! I forgot to say that I also have col3, now if I do df.drop_duplicates(subset = ['col1','col2']), col3 also shows up but i only need col1 and col2 – williamscathy825 Feb 06 '20 at 22:40
  • Thanks AccLok i actually want both columns simultaneously ^-^ – williamscathy825 Feb 06 '20 at 22:42
  • I changed the answer to a list comprehension that returns a list of unique values from every column in your dataframe. – loki Feb 06 '20 at 22:47

2 Answers2

1
df.drop_duplicates(subset = ['col1','col2'])
Mayowa Ayodele
  • 549
  • 2
  • 11
  • 1
    both method worked thank you! the 2nd one returned an array but since i want to join with another df, I just used df.drop_duplicates(subset = ['col1','col2']) then removed col3 in a 2nd step so i am good ^-^ thank you again – williamscathy825 Feb 06 '20 at 22:54
  • @williamscathy825 Glad we could help! For your questions on SO, it would be great if you could up vote the answers you find informative/useful and accept the answer that best suits your requirement. – loki Feb 06 '20 at 23:03
0

[df[x].unique() for x in df.columns] if you are looking for unique values from each column of your dataframe.

loki
  • 976
  • 1
  • 10
  • 22