如果要获取某一列数据有两种方式:

import pandas as pd
series = pd.Series(range(5), index = ['a', 'b', 'c', 'd', 'e'])
df = pd.DataFrame(np.arange(9).reshape(3,3),index = ['a','b','c'],columns = ['A','B','C'])
a = df['A']
b = df.A
print(a, '\n', "*"*30, '\n', b)

输出结果:
在这里插入图片描述
获取某一行的数据:

c = df.loc['a']
d = df.iloc[0]
print(c, '\n', "*"*30, '\n', d)

输出结果
在这里插入图片描述
获取指定列:
在这里插入图片描述

获取连续列:
在这里插入图片描述
获取连续行
在这里插入图片描述
获取指定行
在这里插入图片描述
根据以上方法可以看出:
1:pandas loc 切片索引中是包含末尾值的,列表切片不包含末尾值, iloc也是不包含末尾值的
2:loc 获取指定行列是需要使用 [ ] 进行包裹, 获取连续行列使用 冒号
3 DataFrame中 loc 进行切片,默认使用行,即df[‘a’] 是获取 a 这一行的数据
还有一种方式是使用 iloc 进行切片索引,和 loc 方式类似,iloc 使用 索引编号来索引
例如:
在这里插入图片描述
从这里可以看出,使用 iloc 时,连续索引是不包含末尾值的,其他获取行列方式和 loc 类似

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐