pandas filter

Conditions filter

  • Be filtered through a row loc, can also be assigned to the line after the filter
import pandas as pd
df = pd.DataFrame({"name": ["yang", "wang", "li", "zhang", "zhao"], "score": [100, 78, 112, 61, 94],
"age": [16, 18, 16, 17, 17]})
    • Get name for the yang of the score values:
>>> df.loc[df["name"] == "yang", 'score']

0 100
Name: sorce, dtype: object

    • query method to filter query
df.query("name=='yang'")
   name sorce
0  yang   100

Multi-condition filter

  • Score greater than 90, the age is 17 rows
>>> df[(df.score > 90) & (df.age == 17)]
   name  score  age
4  zhao     94   17
>>>
>>> df.loc[(df.score > 90) & (df.age == 17)] name score age 4 zhao 94 17
# 注意在逻辑操作符两边的过滤条件必须使用小括号括起来,否则条件过滤不起作用
# 过滤后赋值

>>>
mask = (df.score > 90) & (df.age == 17)

mask
0 False
1 False
2 False
3 False
4 True
dtype: bool 

 >>> df.loc[mask, 'score'] = df.loc[mask, 'score'] + 1

 

Guess you like

Origin www.cnblogs.com/spaceapp/p/10937202.html