0

Assume I have a dataframe in Pandas:

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})

The dataframe df looks like

      A      B  C   D
 0  foo    one  0   0
 1  bar    one  1   2
 2  foo    two  2   4
 3  bar  three  3   6
 4  foo    two  4   8
 5  bar    two  5  10
 6  foo    one  6  12
 7  foo  three  7  14

How can I write the code if I want to get the value of D when C equals to 1? In other words, how can I return the D value which is 2 when C = 1?

David
  • 31
  • 5

2 Answers2

0

You can filter and use .loc:

result = df.loc[df.C == 1, "D"]

alternative equivalent syntax as noted in the comments:

result = df.loc[df['C'].eq(1), 'D']

While you can "chain operations, such as df[df.C == 1]["D"] will get you the correct result, you will encounter poor performance as your data scales.

anon01
  • 10,618
  • 8
  • 35
  • 58
-1

You can search the data frame based on condition (here "C" == 1) and then get the column by index lookup

This will return a Series, you will have to use .values to get the NumPy array

>>> df
     A      B  C   D
0  foo    one  0   0
1  bar    one  1   2
2  foo    two  2   4
3  bar  three  3   6
4  foo    two  4   8
5  bar    two  5  10
6  foo    one  6  12
7  foo  three  7  14
>>> df[df["C"] == 1]["D"].values
array([2])
>>> df[df["C"] == 1]["D"].values[0]
2
>>> 

References

tbhaxor
  • 1,659
  • 2
  • 13
  • 43