pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
Pandas是python的一个数据分析包,最初由AQR Capital Management于2008年4月开发,并于2009年底开源出来,目前由专注于Python数据包开发的PyData开发team继续开发和维护,属于PyData项目的一部分。
Pandas最初被作为金融数据分析工具而开发出来,因此,pandas为时间序列分析提供了很好的支持。
Pandas的名称来自于面板数据(panel data)和python数据分析(data analysis)。panel data是经济学中关于多维数据集的一个术语,在Pandas中也提供了panel的数据类型。
主要数据结构为两个类:DataFrame和Series
Series:一维数组,与Numpy中的一维array类似。二者与Python基本的数据结构List也很相近。Series如今能保存不同种数据类型,字符串、boolean值、数字等都能保存在Series中。
Time- Series:以时间为索引的Series。
DataFrame:二维的表格型数据结构。很多功能与R中的data.frame类似。可以将DataFrame理解为Series的容器。
Panel :三维的数组,可以理解为DataFrame的容器。
Panel4D:是像Panel一样的4维数据容器。
PanelND:拥有factory集合,可以创建像Panel4D一样N维命名容器的模块。
# -*- coding: utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者:cacho_37967865
#博客:https://blog.csdn.net/sinat_37967865
#文件:pandas_model.py
#日期:2019-09-06
#备注:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的,主要数据结构为两个类:
DataFrame: 可以理解为表格,类似于Excel的表格 pandas.core.frame.DataFrame
Series: 表示单列。DataFrame包含多个列,即多个Series,每个Series都有名称。pandas.core.series.Series
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''import numpy as np
import pandas as pd# 创建一个pandas对象
def pandas_create():df = pd.DataFrame({"id": [1001, 1002, 1003, 1004, 1005, 1006],"date": pd.date_range('20130102', periods=6),"city": ['Beijing ', 'SH', ' guangzhou ', 'Shenzhen', 'shanghai', 'BEIJING '],"age": [23, 44, 54, 32, 34, 32],"category": ['100-A', '100-B', '110-A', '110-C', '210-A', '130-F'],"price": [1200, np.nan, 2133, 5433, np.nan, 4432]},columns=['id', 'date', 'city', 'category', 'age', 'price'])#print(df,'\n')return df# 查看信息
def pandas_show(df):print(type(df))print('查看数据表基本信息:',type(df.info),'\n',df.info)print('查看维度元组tuple(行数、列数):',df.shape)print('查看每一列数据的格式:',type(df.dtypes),'\n',df.dtypes)print('查看某一列值和数据类型:','\n',df['price'])print('查看数据表的值:',type(df.values),'\n',df.values)print('查看默认前5行数据:','\n',df.head(10))print('查看默认后5行数据:','\n',df.tail())print('查看id列中最大值对应的索引:', '\n', df.id.idxmax(0)) # 只针对数值类型#print('显示所有列最大值所对应的索引:', '\n', df.idxmax(0))print('统计信息展示:','\n',df.describe())print('统计每一列有多少缺失值:', '\n', df.isnull().sum())print('统计每一列非空个数:', '\n', df.count())print('统计某列是否有重复数据:', '\n', df.age.is_unique) # false有重复数据print('按列的值排序:', '\n', df.sort_values(by='age'))print('按顺序进行多列降序排序:', '\n', df.sort_values(['age','category'],ascending=False))print('选择多列数据:', '\n', df[['id','price']])print('选择行数据通过切片获取:', '\n', df[0:3])print('选择行数据通过一个单独列的值来筛选:', '\n', df[df.age > 32])print('行和列转置:', '\n', df.T)print('统计每一列(数字类型)平均值:', '\n', df.mean())print('统计每一列(数字类型)平均值取整:', '\n', round(df.mean()))print('统计每一行(数字类型)平均值:', '\n', df.mean(1))# inplace = True时会改变旧的DataFrame
def pandas_deal(df):#print('删除列改变原来数据:','\n',df.drop('price', axis = 1, inplace = True))print('删除列不改变原来数据:','\n',df.drop('price', axis = 1))print('删除所有均为空值的行:', '\n', df.dropna(how='all'))print('删除包含缺失值的行:','\n',df.dropna())print('填充所有空值(NaN)用数字0:','\n',df.fillna(value=0))print('填充某一列空值(NaN)用数字0:','\n',df.price.fillna(value=0))print('填充某一列空值(NaN)用price均值:','\n',df['price'].fillna(df['price'].mean(),inplace = True))print('修改某个字段的值替换:','\n',df['city'].replace('SH', 'shanghai',inplace = True))print('修改某个字段的值大小写转换','\n',df['city'].str.lower())print('修改某一列的数据格式:','\n',df['age'].astype(float))print('表格值:','\n',df)