根据列值从 Pandas DataFrame 中选择行
要根据列值从DataFrame中选择行,我们可以采取以下步骤-
创建二维、大小可变、潜在异构的表格数据df。
打印输入数据帧。
当x==2时,使用df.loc[df["x"]==2]打印数据帧。
类似地,当(x>=2)和(x<2)时打印DataFrame。
示例
import pandas as pd
df = pd.DataFrame(
{
"x": [5, 2, 1, 9],
"y": [4, 1, 5, 10],
"z": [4, 1, 5, 0]
}
)
print "Given DataFrame is:\n", df
print "When column x value == 2:\n", df.loc[df["x"] == 2]
print "When column x value >= 2:\n", df.loc[df["x"] >= 2]
print "When column x value < 2:\n", df.loc[df["x"] < 2]输出结果Given DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 When column x value == 2: x y z 1 2 1 1 When column x value >= 2: x y z 0 5 4 4 1 2 1 1 3 9 10 0 When column x value < 2: x y z 2 1 5 5
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短