总结

一、Pandas库

  1. import numpy as np
  2. import pandas as pd

二、Pandas库数据结构——Series, DataFrame

  1. a = pd.Series([1, 2, 3, 4, 5])

image.png

  1. data = np.array([[95, 96, 97], [80, 85, 86], [56, 65, 70]])
  2. frame = pd.DataFrame(data)
  3. frame

image.png

1.Series——索引 index,值 values

  1. a = pd.Series([1, 2, 3, 4, 5], index = ['a', 'b', 'c', 'd', 'e'])
  2. a

image.png

2.DataFrame——索引index, columns,值 values

  1. frame = pd.DataFrame(data, index=['xiaoming', 'xiaohong', 'xiaohei'],
  2. columns=['yuwen', 'yingyu', 'shuxue'])
  3. frame

image.png

指定或修改索引方法

创建时:

index, columns 指定索引,已经有索引可以按索引重新排序

创建后:

reindex方法,重新建立索引或指定索引排序

rename 修改索引

  1. frame_.rename(index={"xiaohong":"damao","xiaoming":"ermao","xiaohei":"Nicolas Cage"},
  2. columns={"yingyu":"English", "yuwen":"Literature", "shuxue":"Maths"})

Series.index = []
DataFrame.columns = []

三、Series, DataFrame运算

1.基本运算

按照索引位置进行计算

  1. data = {"English":[80,70,60],
  2. "Literature":[70,70,85],
  3. "Maths":[80,90,50],
  4. "Music":["A","B","C"]}
  5. df = pd.DataFrame(data,index = ["alpha", "beta","theta"])
  6. df * 2

image.png

DataFrame、Series “相加”时,按照DF的columns(列)进行匹配

  1. data1 = {"English":[80,70,60],
  2. "Literature":[70,70,85],
  3. "Maths":[80,90,50],}
  4. df1 = pd.DataFrame(data1,index = ["alpha", "beta","theta"])
  5. add_ = {'Maths':10,'English':10,'Literature':20,'Gym':"A"}
  6. add_ = pd.Series(add_)
  7. df1 + add_

image.png

2.矩阵运算、通用函数

  1. df.T

image.png

3.基本统计方法 axis指定操作轴

  1. df.describe()

image.png

四、Series, DataFrame 索引与切片

1.Series 索引与切片 Index索引/数字索引/布尔值索引

  1. add_ = {'Maths':10,'English':10,'Literature':20,'Gym':"A"}
  2. add_ = pd.Series(add_)
  3. add_['Maths']

image.png

2.DataFrame 索引与切片

  1. Index索引 列:df['Maths'] 行:df.loc[‘alpha’]
  2. 数字索引 df.iloc[] 特别的行可以直接用数字切片索引
  3. 布尔值索引

五、Series, DataFrame 删除操作

1.Series删除操作 pop/drop/del

2.DataFrame删除操作 pop/drop/del

六、Series, DataFrame 合并操作

1.Series合并操作

  1. pd.concat() combine_first()

2.DataFrame合并操作

  1. pd.concat() combine_first()
  2. pd.merge() join()

七、Pandas库其他常用函数或方法

  1. head() info() describe()
  2. sort_index() sort_values()
  3. is_unique value_counts()
  4. rank()