Kora K
Kora K

Reputation: 435

Use boolean series of different length to select rows from dataframe

I have a dataframe that looks like this:

df = pd.DataFrame({"piece": ["piece1", "piece2", "piece3", "piece4"], "No": [1, 1, 2, 3]})

  No   piece
0   1  piece1
1   1  piece2
2   2  piece3
3   3  piece4

I have a series with an index that corresponds to the "No"-column in the dataframe. It assigns boolean variables to the "No"-values, like so:

s = pd.Series([True, False, True, True])

0     True
1    False
2     True
3     True
dtype: bool

I would like to select those rows from the dataframe where in the series the "No"-value is True. This should result in

  No   piece
2   2  piece3
3   3  piece4

I've tried a lot of indexing with df["No"].isin(s), or something like df[s["No"] == True]... But it didn't work yet.

Upvotes: 3

Views: 1522

Answers (2)

Mad Physicist
Mad Physicist

Reputation: 114440

You are trying to index into s using df['No'], then use the result as a mask on df itself:

df[s[df['No']].values]

The final mask needs to be extracted as an array using values because the duplicates in the index cause an error otherwise.

Upvotes: 3

akuiper
akuiper

Reputation: 215047

I think you need map the value in No column to the true/false condition and use it for subsetting:

df[df.No.map(s)]

#  No   piece
#2  2   piece3
#3  3   piece4

df.No.map(s)

# 0    False
# 1    False
# 2     True
# 3     True
# Name: No, dtype: bool

Upvotes: 4

Related Questions