lucky1928
lucky1928

Reputation: 8841

find value of column base on other column condition

I would like to find a value of column A base on column B's condition. for example, find the ts of first 'B' from value columns:

import pandas as pd

data = [[10,"A"],[20,"A"],[30,"B"],[40,"B"]]
df = pd.DataFrame(data,columns=['ts','value'])
print(df)

:    ts value
: 0  10     A
: 1  20     A
: 2  30     B
: 3  40     B

I would like to print out 30 for this example!

Upvotes: 0

Views: 37

Answers (1)

BENY
BENY

Reputation: 323226

You can do that with slice

df.loc[df['value'].eq('B'),'ts'].iloc[0]
Out[163]: 30

Upvotes: 1

Related Questions