博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pandas基本介绍
阅读量:4531 次
发布时间:2019-06-08

本文共 3703 字,大约阅读时间需要 12 分钟。

1、pandas主要的两个数据结构:Series和DataFrame

Series的字符串表现形式为:索引在左边,值在右边。由于我们没有为数据指定索引。于是会自动创建一个0到N-1(N为长度)的整数型索引。

>>> import pandas as pd>>> import numpy as np>>> s = pd.Series([1,3,6,np.nan,44,1])>>> print(s)0     1.01     3.02     6.03     NaN4    44.05     1.0dtype: float64

 DataFrame是一个表格型的数据结构,它包含有一组有序的列,每列可以是不同的值类型(数值,字符串,布尔值等)。DataFrame既有行索引也有列索引, 它可以被看做由Series组成的大字典。

>>> dates = pd.date_range('20160101',periods=6)>>> df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['a','b','c','d'])>>> print(df)                   a         b         c         d2016-01-01  1.306762  1.506943  0.682025 -0.0543292016-01-02  2.626875  0.086998  0.307123 -0.4987282016-01-03 -0.941697  0.206144  1.719719  1.0846142016-01-04 -0.610912 -1.120358 -0.635338  1.1457772016-01-05 -0.150501  0.768586 -0.158341  0.7049602016-01-06 -0.759211  0.271800  0.768166 -0.293015

2、DataFrame的一些简单运用

>>> print(df['b'])2016-01-01    1.5069432016-01-02    0.0869982016-01-03    0.2061442016-01-04   -1.1203582016-01-05    0.7685862016-01-06    0.271800Freq: D, Name: b, dtype: float64>>> df1 = pd.DataFrame(np.arange(12).reshape((3,4)))#创建一组没有给定行标签和列标签的数据 df1:>>> print(df1)   0  1   2   30  0  1   2   31  4  5   6   72  8  9  10  11>>> df2 = pd.DataFrame({
'A' : 1.,... 'B' : pd.Timestamp('20130102'),... 'C' : pd.Series(1,index=list(range(4)),dtype='float32'),... 'D' : np.array([3] * 4,dtype='int32'),... 'E' : pd.Categorical(["test","train","test","train"]),... 'F' : 'foo'})#另一种生成df的方法>>>print(df2) A B C D E F0 1.0 2013-01-02 1.0 3 test foo1 1.0 2013-01-02 1.0 3 train foo2 1.0 2013-01-02 1.0 3 test foo3 1.0 2013-01-02 1.0 3 train foo>>> print(df2.dtypes)#查看数据中的类型A float64B datetime64[ns]C float32D int32E categoryF objectdtype: object>>> print(df2.index) #查看对列的序号Int64Index([0, 1, 2, 3], dtype='int64')>>> print(df2.columns)#查看每种数据的名称Index(['A', 'B', 'C', 'D', 'E', 'F'], dtype='object')>>> print(df2.values)#查看所有df2的值[[1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo'] [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo'] [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'test' 'foo'] [1.0 Timestamp('2013-01-02 00:00:00') 1.0 3 'train' 'foo']]>>> df2.describe()#数据的总结 A C Dcount 4.0 4.0 4.0mean 1.0 1.0 3.0std 0.0 0.0 0.0min 1.0 1.0 3.025% 1.0 1.0 3.050% 1.0 1.0 3.075% 1.0 1.0 3.0max 1.0 1.0 3.0>>> print(df2.T)#翻转数据,transpose 0 1 2 3A 1 1 1 1B 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00 2013-01-02 00:00:00C 1 1 1 1D 3 3 3 3E test train test trainF foo foo foo foo>>> print(df2.sort_index(axis=1, ascending=False))#对数据的index进行排序并输出 F E D C B A0 foo test 3 1.0 2013-01-02 1.01 foo train 3 1.0 2013-01-02 1.02 foo test 3 1.0 2013-01-02 1.03 foo train 3 1.0 2013-01-02 1.0>>> print(df2.sort_values(by='B'))#对数据值排序输出 A B C D E F0 1.0 2013-01-02 1.0 3 test foo1 1.0 2013-01-02 1.0 3 train foo2 1.0 2013-01-02 1.0 3 test foo3 1.0 2013-01-02 1.0 3 train foo

 

转载于:https://www.cnblogs.com/anhoo/p/9383664.html

你可能感兴趣的文章
php用户名密码
查看>>
Mysql 更改最大连接数
查看>>
sqlserver 2012 重启是 ID 自动增长 1000的问题
查看>>
oracle用户连接失败
查看>>
json粗浅认识
查看>>
java学习笔记之基础知识
查看>>
电子商务(电销)平台中商品模块(Product)数据库设计明细(转载)
查看>>
docker 练习
查看>>
day 18
查看>>
IOS开发学习---自动调整旋转和调整大小的动画效果
查看>>
动态路由的意义,以及路由重定向
查看>>
【转】sqlserver使用sql导出索引
查看>>
图论专题II
查看>>
Java占位符替换工具类
查看>>
<strong>和 <b> 的区别
查看>>
HTML5 20180919
查看>>
后端程序员之路 12、K最近邻(k-Nearest Neighbour,KNN)分类算法
查看>>
缓存技术
查看>>
linux64需要增加的依赖库
查看>>
手机网站页面模板
查看>>