假设我们有如下的DataFrame:
import pandas as pd
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
'age': [25, 28, 21, 32, 24],
'gender': ['F', 'M', 'M', 'M', 'F'],
'score': [80, 90, 85, 95, 87]
}
df = pd.DataFrame(data)
方法一:使用布尔索引
# 选取年龄大于25岁的行
df[df['age'] > 25]
方法二:使用query方法
# 选取得分大于等于85分的行
df.query('score >= 85')
方法三:使用loc方法
# 选取性别为女性的行
df.loc[df['gender'] == 'F']
方法四:使用isin方法
# 选取名字为Alice和David的行
df[df['name'].isin(['Alice', 'David'])]
civilpy:Python数据分析及可视化实例目录