Amisha Mishra
Amisha Mishra

Reputation: 35

Python DataFrame - Select dataframe rows based on values in a column of same dataframe

I'm struggling with a dataframe related problem.

columns = [desc[0] for desc in cursor.description]
data = cursor.fetchall()
df2 = pd.DataFrame(list(data), columns=columns)

df2 is as follows:

| Col1     | Col2           |
| -------- | -------------- |
| 2145779  | 2              |
| 8059234  | 3              |
| 2145779  | 3              |
| 4265093  | 2              |
| 2145779  | 2              |
| 1728234  | 5              |

I want to make a list of values in col1 where value of col2="3"

Upvotes: 2

Views: 70

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

You can use boolean indexing:

out = df2.loc[df2.Col2.eq(3), "Col1"].agg(list)
print(out)

Prints:

[8059234, 2145779]

Upvotes: 4

Related Questions