2

With the following code I get:

import pandas as pd

date=['1/3/15','2/5/15','3/6/15','4/8/16']
dist=[5,4,11,12]
dd=list(zip(date,dist))
df=pd.DataFrame(dd,columns=['Date','Dist'])
print(df)

out:

Date  Dist
0  1/3/15     5
1  2/5/15     4
2  3/6/15    11
3  4/8/16    12

I would like to be able to get only the dist > 10 and the corresponding date as so:

Date  Dist
2  3/6/15  11
3  4/8/16  12

I've tried the following:

dd10=pd.DataFrame(df['Dist']>10)
print(dd10)

Which only results in:

0    False
1    False
2     True
3     True
Name: Dist, dtype: bool

How can I get the desired result as an int. with the corresponding date instead of a bool?

wanderweeer
  • 449
  • 1
  • 6
  • 16

1 Answers1

1

It is called boolean indexing and need df[mask]:

df1 = df[df['Dist']>10]

Another way for filtering is DataFrame.query:

df1 = df.query("Dist > 10")

print (df1)
     Date  Dist
2  3/6/15    11
3  4/8/16    12
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252