pandas快速入门

开源的数据挖掘库

用于数据探索

封装了matplotlib,numpy

案例知识点

pd.DataFrame(ndarray)–创建DataFrame

pd.date_range()–创建日期

参数: start–开始日期

​ end–结束日期

​ perios–时间跨度

​ freq –统计时间方式

stock_change=np.random.normal(0,1,(10,5))
stock_change
stock_code=["第{}股票".format(i+1) for i in range(stock_change.shape[0])]
stock_time=pd.date_range(start="20210103",periods=stock_change.shape[1])
stock_c=pd.DataFrame(stock_change,index=stock_code, columns=stock_time)
stock_c

DataFrame

对象.shape
对象.values
对象.index
对象.columns
对象.T --转置
对象.head --查看前几行
对象.tail --查看后几行

DataFrame设置索引

必须整行或者整列去进行修改,直接通过属性更改

stock_i=["股票{}".format(i+1) for i in range(stock_change.shape[0])]
stock_c.index=stock_i
stock_c

对象.reset_index()

stock_c.reset_index(drop=True)

索引操作

算术运算

方法add,sub…

也可以直接用符号

import pandas as pd
data=pd.read_csv("./data/test.csv")
data.head()
data["close"].add(4).head()

逻辑运算

  • dataframe.query(“”)
  • series.isin
data.query("open>23.53 & high>25").head()
data["open"].isin([23.5])
data[data["open"].isin([23.5])].head()

统计函数

自定义函数

apply(func,axis=0)

定义一个对列,最大值-最小值的函数

data[["open", "high", "close"]].apply(lambda x: x.max()-x.min())

pandas画图

DataFrame.plot()

参数:kind

  • line–折线图
  • bar
  • barh–条形图
  • hist–直方图
  • pie
  • scatter

文件读取和存储

缺失值-处理

数据离散化

数据合并

pd.concat() –参数axis=

pd.concat([left,right],join='inner',axis=1,ignore_index=0)

pd.merge()

​ left –左表

​ right –右表

​ on –指定键

​ how – 按照什么方式进行拼接

pd.merge(left,right,how="inner",on=["key1","key2"])

数据透视表

分组和聚合

Original: https://blog.csdn.net/qq_44850917/article/details/122744759
Author: –believe
Title: pandas快速入门

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/677193/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球