浙江技术高考 Pandas & plt 模块笔记

· · 学习·文化课

\texttt{I Pandas}

\mathcal{1 \ 数据类型}

series Excel 内一列(一维)

dataframe 二维表格

\mathcal{2 \ 用法}

\mathcal{2.1 \ 基础}

import pandas as pd 有手就行

df = pd.read_excel("[path]") 读 Excel

df.to_excel("name.xls") 存到 Excel 中

\mathcal{2.2 \ 访问}

df.index 返回一个 range 类型参数,为行标题

df.columns 返回一个 list 类型参数,为列标题,也可用 df.columns(start:end),选取一个范围内列标题

df.values 返回一个 list 类型参数,外层 list 其中每一个元素都是一个 list,为一行的所有数据

df["{name}"]df.{name} 访问单列数据

df[[{name1},{name2}...]] 访问多列数据

df[start:end] 查询从 \texttt{start}\texttt{end-1} 行的数据

df.at[行标签号,列标题]df[列标题][行号(此处可以使用切片)] 查询单个数据

df[{list}].sum(axis={0/1}){list} 内列求和(axis=0 表示竖着求和,axis=1 反之)

df[{string}][start:end].count() 查询某一列一段区间内非空个数

df[{条件}]df.{条件},返回一个 dataframe 类型参数。条件可为 (条件1) {&/|} ({条件2})

df.head(int1) df.tail(int1) 选出前 int1 或后 int1

\mathcal{2.3 \ 修改}

df = df.drop({list},axis=0),返回一个 dataframe 类型参数。列表为需删除的行号,axis=0 表示删除行

df = df.drop({list},axis=1),列表为需删除列的列标题,axis=1 同理

df.insert({int},{string},{series}) 分别表示插入到第几列,列标题,列内容

df = df.sort_values({string},ascending = {True/False}) 按照 {string} 关键字排序,ascending = False 降序,ascending = True 升序。也可以 sort_values([{string1},{string2}...],asending = [bool1,bool2...]) 多关键字排序,表示以 {string1} 为第一关键字,按照 bool1 顺序,{string2} 为第二关键字,按照 bool2 顺序排序...

df = df.groupby({string1},as_index = {True/False})string1 为关键字进行分组,as_index=Truestring1 下内容为新 index,否则自动从 0 开始排。如果使用 df.gruopby({string1},ans_index = True)[{string2}].{sum()/mean()...} 时会返回一个 Series 而非 Dataframe

df1 = df1.append(df2,ignore_index=True/False) 将两个 Dataframe 合并,ignore_index=True 时重新编排 index ,否则使用原 index (有可能重复)

\texttt{II plt}

\mathcal{1 \ 基础}

import matplotlib.pyplot as plt

plt.savefig({string}) 保存

plt.show() 展示

\mathcal{2 \ 绘图}

plt.figure(figsize=(int1,int2)) 创建画布大小,单位为英寸

plt.title({string}) 标题

plt.{plot,bar,scatter}({x,y},label={string}) 折线图 柱状图 散点图(x,y 均为 list,标签为 stirng

plt.xlabel({string}) plt.ylabel({string}) 设置 x/y 轴标题

plt.xlim plt.ylim 设置 x/y 轴显示范围

\color{white}\small{金政喜欢沈钊如} \color{lightgray}\small{tanger 喜欢黑面包}