numpy- pandas-matplotlib

numpy模块

  • 多维数组(列表)的运算
import numpy as np

arr1 = np.array([ [1,2,3,4],[5,6,7,8] ])  # numpy数组
arr2 = np.array([ [1,2,3,4],[5,6,7,8] ])  # numpy数组

arr3 = [ [1,2,3,4],[5,6,7,8] ]   # 列表

arr3[0]  # [1,2,3,4]

arr1[0,0]  # 0

arr1[0,:]  # [1,2,3,4]

arr1[0,[0,1,2,3]]  # [1,2,3,4]

arr1.dtype
arr1.shape
arr1.min()
arr1.max()

arr = np.random.rand(3, 4) # 生成3*4的二维数组

# axis = 0 列
# axis = 1 行

pandas模块

  • 处理表格等文件/数据库
    • 获取数据(自定义,未来爬虫获取)
    • 对数据逻辑处理,pandas+numpy
    • 保存数据
import numpy as np
import pandas as pd

# 获取数据

arr_values = np.random.rand(3,4)

dates_index = pd.date_range('2019-01-01',periods=3)

goods_columns = ['tesla','chongqiwawa','transformer','masaladi']


df = pd.DataFrame(arr_values, index=dates_index, columns=goods_columns)
print(df)

'''
               tesla  chongqiwawa  transformer  masaladi
2019-01-01  0.108903     0.770686     0.512011  0.930160
2019-01-02  0.858702     0.324466     0.429598  0.036194
2019-01-03  0.636926     0.420314     0.212146  0.013478
'''

# 逻辑处理

df.iloc[0,0] = np.nan
print(df)

'''
               tesla  chongqiwawa  transformer  masaladi
2019-01-01       NaN     0.770686     0.512011  0.930160
2019-01-02  0.858702     0.324466     0.429598  0.036194
2019-01-03  0.636926     0.420314     0.212146  0.013478
'''

print(df.dropna(axis=0))  # 行

'''
               tesla  chongqiwawa  transformer  masaladi
2019-01-02  0.858702     0.324466     0.429598  0.036194
2019-01-03  0.636926     0.420314     0.212146  0.013478
'''

print(df.dropna(axis=1))  # 列

'''
            chongqiwawa  transformer  masaladi
2019-01-01     0.770686     0.512011  0.930160
2019-01-02     0.324466     0.429598  0.036194
2019-01-03     0.420314     0.212146  0.013478
'''


# 保存文件

df.to_excel('test.xlsx')

matplotlib模块

  • 画图/数据可视化

一张画布

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# %matplotlib inline
font = FontProperties(fname='字体路径')


plt.bar()  # 条形图
plt.plot()  # 直线图
plt.hist()  # 直方图
plt.scatter()  # 散点图
plt.text()  # 文本

plt.show()

两张画布

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# %matplotlib inline
font = FontProperties(fname='D:\msyh.ttc')

fig = plt.figure(figsize=(12,12))

ax1 = fig.add_subplot(121)
ax1.text(0.5,0.5,'121画布',fontproperties=font)

ax2 = fig.add_subplot(122)
ax2.text(0.5,0.5,'122画布',fontproperties=font)

ax1.set_title()  # ax1的标题
ax2.set_title()  # ax2的标题
fig.suptitle()  # 总标题

plt.show()

猜你喜欢

转载自www.cnblogs.com/zhuyuanying123--/p/11015248.html