242

I am kind of getting stuck on extracting value of one variable conditioning on another variable. For example, the following dataframe:

A  B
p1 1
p1 2
p3 3
p2 4

How can I get the value of A when B=3? Every time when I extracted the value of A, I got an object, not a string.

2
  • 1
    I see, I should add item() at the end. Commented Apr 18, 2016 at 1:23
  • df.query and pd.eval seem like good fits for this use case. For information on the pd.eval() family of functions, their features and use cases, please visit Dynamic Expression Evaluation in pandas using pd.eval(). Commented Dec 16, 2018 at 4:58

7 Answers 7

397

You could use loc to get series which satisfying your condition and then iloc to get first element:

In [2]: df
Out[2]:
    A  B
0  p1  1
1  p1  2
2  p3  3
3  p2  4

In [3]: df.loc[df['B'] == 3, 'A']
Out[3]:
2    p3
Name: A, dtype: object

In [4]: df.loc[df['B'] == 3, 'A'].iloc[0]
Out[4]: 'p3'
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks for your help. df.loc[df['B'] == 3, 'A'].item() works for me too.
which one does it pick if the data frame has multiple entries of '3' in column B?
df.loc[df["Condition_Column"] == 0, ("Column_1", "Column_2, "Column_3", "Column_4")] works for me. This example for select multiple columns. They should be in tuple.
.item() apparently has been deprecated and will be removed. Is there another way to do this? I'm not interested in the name of the column or datatype which is also returned with the .loc method to query.
@user1999109 if the condition doesn't have any matches (in your example, 300 is never found in column B), then the part before .iloc will return an empty series. If you then try to retrieve a value at a non-existent index (index 0), it returns an IndexError.
|
93

You can try query, which is less typing:

df.query('B==3')['A']

3 Comments

Query is interesting cause we can add more complex clauses to it as well
IMHO, this is the best answer.
@NLR It depends. query creates a new DataFrame, and then the slice ['A'] creates yet another DataFrame. For large cases one would like to avoid the unnecessary intermediate object.
69

Try:

df[df['B']==3]['A'].item()

assuming df is your pandas.DataFrame.

4 Comments

Can you please give a link where exactly this method is described in pandas official documentation?
I mean ][ part.
@vasili111 check pandas filter
Add a .item() in the end.
23

Use df[df['B']==3]['A'].values[0] if you just want item itself without the brackets

3 Comments

Can you please give a link where exactly this method is described in pandas official documentation? I mean ][ part.
It does return the result with []
You still need to put [0] at the end to access the value.
15

Edited: What I described below under Previous is chained indexing and may not work in some situations. The best practice is to use loc, but the concept is the same:

df.loc[row, col]

row and col can be specified directly (e.g., 'A' or ['A', 'B']) or with a mask (e.g. df['B'] == 3). Using the example below:

df.loc[df['B'] == 3, 'A']

Previous: It's easier for me to think in these terms, but borrowing from other answers. The value you want is located in a dataframe:

df[*column*][*row*]

where column and row point to the values you want returned. For your example, column is 'A' and for row you use a mask:

df['B'] == 3

To get the first matched value from the series there are several options:

df['A'][df['B'] == 3].values[0]
df['A'][df['B'] == 3].iloc[0]
df['A'][df['B'] == 3].to_numpy()[0]

Comments

3

You can use squeeze instead of iloc[0]. It looks clearer if you have only one value:

df.loc[df['B'] == 3, 'A'].squeeze()

Output:

'p3'

Comments

0
df.loc[df['B']=='give-specific-value', 'A']

I have also worked on this clausing and extraction operations for my assignment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.