-1

I need to find unique name, whose age=2 and and cond=9 using python pandas?

name age cond cc
a 2 9 3
b 2 8 2
c 3 9 1
a 2 9 6
Jack
  • 181
  • 10
  • 1
    Does this answer your question? [How to filter Pandas dataframe using 'in' and 'not in' like in SQL](https://stackoverflow.com/questions/19960077/how-to-filter-pandas-dataframe-using-in-and-not-in-like-in-sql) – INGl0R1AM0R1 Jun 24 '22 at 17:49

3 Answers3

1

This will find all distinct rows where age = 2 and cond = 9

df.loc[(df['age'] == 2) & (df['cond'] == 9)][['name', 'cc']].drop_duplicates()
ArchAngelPwn
  • 2,891
  • 1
  • 4
  • 17
1

The Pandas query function allows for SQL-like queries to filter a data frame. Then use unique() on the results to return the unique name.

rows = df.query('age == 2 and cond == 9')
print(rows["name"].unique())

For more query examples, see here.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75
0

One potential solution is to put the columns in a zip() and then iterate through your dataframe like so:

for name, age, cond in zip(df['name'], df['Age'], df['cond']):
    if(age == 2 and cond ==9):
      print(name)
pppery
  • 3,731
  • 22
  • 33
  • 46