Python数据分析 知识量:13 - 56 - 232
head()函数用于预览DataFrame数据,默认是查看前5行数据,可以通过参数指定查看的行数。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam.xlsx") print(df,'\n') # DataFrame原数据 print(df.head(),'\n') # 查看前5行(默认) print(df.head(3)) # 查看前3行
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Noah male 90 50 66 3 Olivia female 86 87 44 4 Liam male 55 88 69 5 Sophia female 90 66 96 6 Liam male 55 88 69 7 Isabella female 66 85 55 Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Noah male 90 50 66 3 Olivia female 86 87 44 4 Liam male 55 88 69 Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Noah male 90 50 66
shape()函数用于查看数据对象的形状,即几行几列,结果以元组的形式返回。在计算时,Python不会把行列索引计算在内。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam.xlsx") print(df) print(df.shape)
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Noah male 90 50 66 3 Olivia female 86 87 44 4 Liam male 55 88 69 5 Sophia female 90 66 96 6 Liam male 55 88 69 7 Isabella female 66 85 55 (8, 5)
元组(8,5)表示数据对象是8行5列。
info()函数用于查看数据对象中的数据类型。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam.xlsx") df.info()
运行结果为:
<class 'pandas.core.frame.DataFrame'> RangeIndex: 8 entries, 0 to 7 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Name 8 non-null object 1 Sex 8 non-null object 2 Chinese 8 non-null int64 3 English 8 non-null int64 4 Math 8 non-null int64 dtypes: int64(3), object(2) memory usage: 448.0+ bytes
数据表中行索引(index)是0~7(8行),共5列(columns),2列类型是object,3列类型是int64,共占用448.0字节。
数值分布可以展示数据的均值、最值、方差、分位数等信息,是初步的数据分析。describe()函数用于查看数据分布。注意:该函数只对数值数据起作用。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam.xlsx") print(df.describe())
运行结果为:
Chinese English Math count 8.00000 8.0000 8.000000 mean 73.50000 71.2500 65.000000 std 16.98739 17.5723 15.287717 min 55.00000 50.0000 44.000000 25% 55.75000 54.5000 55.000000 50% 76.00000 75.5000 66.000000 75% 90.00000 87.2500 69.000000 max 90.00000 88.0000 96.000000
Copyright © 2017-Now pnotes.cn. All Rights Reserved.
编程学习笔记 保留所有权利
MARK:3.0.0.20240214.P35
From 2017.2.6