2

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"

1 Answer 1

4

You can use boolean indexing:

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

Prints:

[8059234, 2145779]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.