Python 数据分析 - 基础技术(持续 ing)

文章目录

      • 第一部分 Python 数据分析概述
      • 第二部分 numpy
      • 第三部分 matplotlib
      • 第四部分 scipy
      • 第五部分 pandas
      • 第六部分 scikit-learn
      • 第七部分 keras

第一部分 Python 数据分析概述

  • 数据分析的含义与目标
    • 方法:统计分析方法
    • 用处:提取有用信息
    • 研究、概括、总结
  • Python 数据分析包
    • numpy:数据结构基础
    • scipy: 强大的科学计算方法(矩阵分析、信号分析、数理分析等)
    • matplotlib:丰富的可视化套件
    • pandas:基础数据分析套件
    • scikit-learn:强大的数据分析建模库
    • keras:人工神经网络

第二部分 numpy

numpy

第三部分 matplotlib

第四部分 scipy

第五部分 pandas

pandas 官方文档

"""
1、data structure
"""
# 类似一维数组
s = pd.Series([i*2 for i in range(1, 11)])
print(type(s))
dates = pd.bdate_range("20190909", periods=8)
df = pd.DataFrame(np.random.randn(8, 5), index=dates, columns=list("ABCDE"))
print(df)

"""
2、基础操作
"""
# 打印前三行
print(df.head(3))
# 打印后几行
print(df.tail(3))
# 打印下标
print(df.index)
# 打印值
print(df.values)
# 打印转置
print(df.T)
# 打印排序,针对 C 列
print(df.sort_values("C", inplace=False))
# 对 index 排序,降序
print(df.sort_index(axis=1, ascending=False))
# 打印 max、min等
print(df.describe())

# select
print(type(df["A"]))
print(df[:3])
print(df["20190909":"20190912"])
print(df.loc[dates[0]])
print(df.loc["20190909":"20190912", ["B", "D"]])
print(df.at[dates[0], "C"])
print(df.iloc[1:3, 2:4])
print(df.iloc[1, 4])
print(df.iat[1, 4])
print(df[(df.B > 0) & (df.A > 0)])
print(df[df > 0])
print(df[df["E"].isin([1, 2])])

dataframe 多列数据筛选

# set
# 有错误!!!!!!!!!
index = pd.date_range("20190909", periods=8)
s1 = pd.Series(list(range(10, 18)).index(index))
print(s1)

df["F"] = s1
print(df)
df.at[dates[0], "A"]
print(df)
df.iat[1, 1] = 1
df.loc[:, "D"] = np.array([4]*len(df))
print(df)
df2 = df.copy()
df2[df2 > 0] = -df2
print(df2)

"""
3、缺失值处理
"""
df1 = df.reindex(index=dates[:4], columns=list("ABCD")+["G"])
df1.loc[dates[0]:dates[1], "G"] = 1
print(df1)
# 丢弃
print(df1.dropna())
# 填充
print(df1.fillna(value=1))

"""
4、表的拼接和数据整合
"""
# 均值
print(df.mean())
# 方差
print(df.var())
s = pd.Series([1, 2, 4, np.nan, 5, 7, 9, 10], index=dates)
print(s)
# 将所有值往后移两个,前边空值不会补全
print(s.shift(2))
print(s.diff())
# 打印每个值在 Series 出现的次数
print(s.value_counts())
print(df.apply(np.cumsum))
print(df.apply(lambda x:x.max()-x.min()))

# 表格拼接
pieces = [df[:3], df[-3:1]]
print(pd.concat(pieces))
left = pd.DataFrame({
     "key": ["x", "y"], "value": [1, 2]})
right = pd.DataFrame({
     "key": ["x", "z"], "value": [3, 4]})
print("LEFT: ", left)
print("RIGHT: ", right)
print(pd.merge(left, right, on="key", how="left"))
print(pd.merge(left, right, on="key", how="inner"))
print(pd.merge(left, right, on="key", how="outer"))
df3 = pd.DataFrame({
     "A": ["a", "b", "c", "b"], "B": list(range(4))})
print(df3.groupby("A").sum())

"""
5、时间、绘图、文件操作
"""
# time series
t_exam = pd.date_range("20190301", periods=10, freq="S")
print(t_exam)

# Graph
# 有错误!!!!!!!!!
ts = pd.Series(np.random.randn(1000, index=pd.date_range("20190301", periods=1000)))
ts = ts.cumsum()
# from pylab import *
# ts.plot()
# show()
# 文件操作
data = pd.read_csv("./崇山兰若图轴 王铎.txt")
print(data)
data.to_csv("./test1.csv")
# data_excel = pd.read_excel("./123.excel", "Sheet1")
# print(data_excel)
# 引入 openpyxl 模块
# data_excel.to_excel("./test2.xlsx")

第六部分 scikit-learn

第七部分 keras

你可能感兴趣的:(Python 数据分析 - 基础技术(持续 ing))