引言
Python数据分析活用Pandas库学习笔记,博客中涉及的数据文件,下载下面的连接
数据资料链接:https://www.ituring.com.cn/book/download/37deb472-af30-4444-b791-92f983fe00c9
第1章 Pandas DataFrame 基础知识
"""
2021.02.18
author:alian
"""
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv(r'D:\Python数据分析\Pandas\pandas_for_everyone-master\data\gapminder.tsv',sep='\t')
sub_df = df['country']
sub_set = df[['country','continent','year']]
row1_df = df.loc[0]
rowlast_df = df.tail(n=1)
row0_df = df.iloc[0]
row_df= df.iloc[-1]
group = df.groupby('year')['lifeExp'].mean()
print(group)
group.plot()
plt.show()
第2章 Pandas数据结构
"""
2021.02.18
author:alian
Pandas 数据结构
Series相当于python内置的列表;DataFrame相当于python内置的字典
"""
import pandas as pd
import matplotlib as plt
import random
s = pd.Series(['banada','42'])
scientists = pd.DataFrame({
'Name':['RosalineFranklin','William Gosset'],
'Occupation':['Chemist','Statistician'],
'Born':['1920-07-25','1876-06-13'],
'Died':['1958-04-16','1937-10-16'],
'Age':[37,61]
})
scientists1 = pd.DataFrame(
data={
'Name':['RosalineFranklin','William Gosset'],
'Occupation':['Chemist','Statistician'],
'Born':['1920-07-25','1876-06-13'],
'Died':['1958-04-16','1937-10-16'],
'Age':[37,61]
},
index = ['RosalineFranklin','William Gosset'],
columns = ['Occupation','Born','Died','Age'])
row = scientists1.loc['William Gosset']
scientists = pd.read_csv(r'D:\Python数据分析\Pandas\pandas_for_everyone-master\data\scientists.csv')
ages = scientists['Age']
bool_index = [True,True,False,False,True,True,False,True]
born_datatime = pd.to_datetime(scientists['Born'],format='%Y-%m-%d')
random.seed(42)
random.shuffle(scientists['Age'])
scientists['Age'] = scientists['Age'].sample(len(scientists['Age']),random_state=24).reset_index(drop=True)
print(scientists['Age'])
scientists_dropped = scientists.drop(['Age'],axis=1)
2.1 Serise
2.1.1 Serise的属性
Serise属性 | 说明 |
---|
loc | 使用索引标签取子集 |
iloc | 使用索引号取子集 |
dtype或dtypes | Serise内容的类型 |
T | Serise的转置矩阵 |
shape | 数据维度 |
size | Serise元素的数量 |
values | 类似与ndarray的Serise |
2.1.2 Serise的方法
Serise方法 | 说明 |
---|
describe | 获取Serise 的基本统计量,包括数量、均值、最大值、最小值等 |
append | 连接两个或多个Serise |
corr | 计算与另一个Serise的相关系数* |
cov | 计算与另一个Serise的协方差* |
describe | 计算概括统计量* |
drop_duplicates | 返回一个不含重复项的Serise |
equals | 判断两个Serise是否有相同元素 |
get_values | 获取Serise的值,功能和values属性相同 |
hist | 绘制直方图 |
isin | 逐个检查Serise中的每个元素是否存在于参数指定的序列中 |
min | 返回最小值 |
max | 返回最大值 |
mean | 返回平均数 |
std | 返回标准差 |
median | 返回中位数 |
mode | 返回众数 |
quantile | 返回指定位置的四分位数 |
replace | 用指定值代替Serise中的值 |
sample | 返回Serise的随机采样值 |
sort_values | 对值进行排序 |
to_frame | 把Serise转换为DataFrame |
transpose | 返回转置矩阵 |
unique | 返回由唯一值组成的numpy.ndarray |
2.2 DataFrame
获取DataFrame子集的一些方法
方法 | 执行结果 |
---|
df[column_name] | 单列 |
df[[column1,column2,…]] | 多列 |
df.loc[row_label] | 使用行标签(行名)获取数列行 |
df.loc[[label1,label2,…]] | 使用索引标签获取多行 |
df.iloc[row_number] | 使用行号获取数据行 |
df.iloc[[row1,row2,…]] | 使用行号获取多行 |
df[bool] | 使用布尔值获取行 |
df[[bool1,bool2,…]] | 使用布尔值获取多行 |
df[start:stop:step] | 使用切片方法获取数据行 |
导出数据的方法
导出方法 | 说明 |
---|
to_pickle | 把数据保存为二进制格式 |
to_csv | 把数据保存为csv格式 |
to_excel | 把数据保存为excel格式 |
to_feather | 将数据储存二进制对象 |
to_clipboard | 把数据保存到系统剪贴板,方便粘贴 |
to_dense | 把稀疏对象转换为标准密集形式 |
to_dict | 把数据转换为标准python字典 |
to_gbq | 把数据转换为Goolgle BigQuery表 |
to_hdf | 把数据保存为HDP格式 |
to_msgpack | 把数据保存为类似JSON的便携二进制格式 |
to_html | 把数据转换为HTML表 |
to_json | 把数据转换成JSON字符串 |
to_latex | 把数据转换成LaTex表格环境 |
to_records | 把数据转换成记录数组 |
to_string | 把标准输出以字符串形式显示DataFrame |
to_sparse | 把数据转换为SparceDataFrame |
to_aql | 把数据保存到SQL数据库 |
to_stata | 把数据转换成Stata dta文件 |