dataFrame中的值以矩阵的形式存在,在访问值时需要带上行索引或者列索引。
1、dataFrame简单的取值方法
import pandas as pd
def createDataFrame():
d = {
‘a’:[1,2,3,4,5],
‘b’:[6,2,3,6,0],
‘c’:[4,2,3,6,7],
‘d’:[5,3,2,4,5],
‘e’:[6,7,4,5,8]
df = pd.DataFrame(d)
打印出dataFrame
print(df)
if name == ‘main‘:
createDataFrame()
(1)输出前3行数据
print(df.head(3))
打印结果为:
a b c d e
0 1 6 4 5 6
1 2 2 2 3 7
2 3 3 3 2 4
(2)输出后2行数据:
print(df.tail(2))
打印结果为:
a b c d e
3 4 6 6 4 5
4 5 0 7 5 8
(3)输出第3行数据:
loc通过标签来选择数据
print(df.loc[2])#这里的2表示行索引
iloc通过位置来选择数据
print(df.iloc[2])#这里的2表示行位置
打印结果
a 3<
Original: https://blog.csdn.net/weixin_31233537/article/details/112864824
Author: 杨奎元-深动
Title: series选取值_pandas中dataFrame的取值和计算操作
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/674938/
转载文章受原作者版权保护。转载请注明原作者出处!