Python Pandas - 如何从 DataFrame 中选择多行
要从DataFrame中选择多行,请使用:运算符设置范围。首先,使用别名导入requirepandas库-
import pandas as pd
现在,创建一个新的PandasDataFrame-
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]],index=['w', 'x', 'y', 'z'],columns=['a', 'b'])
使用:操作符选择多行-
dataFrame[0:2]
示例
以下是代码-
import pandas as pd #CreateDataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]],index=['w', 'x', 'y', 'z'],columns=['a', 'b']) #DataFrame print"DataFrame...\n",dataFrame #selectrowswithloc print"\nSelect rows by passing label..." print(dataFrame.loc['z']) #selectrowswithintegerlocationusingiloc print"\nSelect rows by passing integer location..." print(dataFrame.iloc[1]) #selectingmultiplerows print"\nSelect multiple rows..." print(dataFrame[0:2])输出结果
这将产生以下输出-
DataFrame...
a b
w 10 15
x 20 25
y 30 35
z 40 45
Select rows by passing label...
a 40
b 45
Name: z, dtype: int64
Select rows by passing integer location...
a 20
b 25
Name: x, dtype: int64
Select multiple rows...
a b
w 10 15
x 20 25热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短