到目前为止,我们了解了三种Pandas数据结构以及如何创建它们。接下来将主要关注数据帧(DataFrame)对象,因为它在实时数据处理中非常重要,并且还讨论其他数据结构。

系列基本功能

编号 属性或方法 描述
1 axes 返回行轴标签列表。
2 dtype 返回对象的数据类型(dtype)。
3 empty 如果系列为空,则返回True。
4 ndim 返回底层数据的维数,默认定义:1。
5 size 返回基础数据中的元素数。
6 values 将系列作为ndarray返回。
7 head() 返回前n行。
8 tail() 返回最后n行。

现在创建一个系列并演示如何使用上面所有列出的属性操作。

示例

import pandas as pd
import numpy as np #Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print s
Python

执行上面示例代码,得到以下输出结果 -

0   0.967853
1 -0.148368
2 -1.395906
3 -1.758394
dtype: float64
Python

axes示例

返回系列的标签列表。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("The axes are:")
print s.axes
Python

执行上面示例代码,得到以下输出结果 -

The axes are:
[RangeIndex(start=0, stop=4, step=1)]
Python

上述结果是从0到5的值列表的紧凑格式,即:[0,1,2,3,4]。

empty示例

返回布尔值,表示对象是否为空。返回True则表示对象为空。

import pandas as pd
import numpy as np #Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print ("Is the Object empty?")
print s.empty
Python

执行上面示例代码,得到以下输出结果 -

Is the Object empty?
False
Python

ndim示例

返回对象的维数。根据定义,一个系列是一个1D数据结构,参考以下示例代码 -

import pandas as pd
import numpy as np #Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s print ("The dimensions of the object:")
print s.ndim
Python

执行上面示例代码,得到以下结果 -

0   0.175898
1 0.166197
2 -0.609712
3 -1.377000
dtype: float64 The dimensions of the object:
1
Shell

size示例

返回系列的大小(长度)。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a series with 4 random numbers
s = pd.Series(np.random.randn(2))
print s
print ("The size of the object:")
print s.size
Python

执行上面示例代码,得到以下结果 -

0   3.078058
1 -1.207803
dtype: float64 The size of the object:
2
Shell

values示例

以数组形式返回系列中的实际数据值。

import pandas as pd
import numpy as np #Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print s print ("The actual data series is:")
print s.values
Python

执行上面示例代码,得到以下结果 -

0   1.787373
1 -0.605159
2 0.180477
3 -0.140922
dtype: float64 The actual data series is:
[ 1.78737302 -0.60515881 0.18047664 -0.1409218 ]
Shell

head()和tail()方法示例

要查看Series或DataFrame对象的小样本,请使用head()和tail()方法。

head()返回前n行(观察索引值)。要显示的元素的默认数量为5,但可以传递自定义这个数字值。

import pandas as pd
import numpy as np #Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print ("The original series is:")
print s print ("The first two rows of the data series:")
print s.head(2)
Python

执行上面示例代码,得到以下结果 -

The original series is:
0 0.720876
1 -0.765898
2 0.479221
3 -0.139547
dtype: float64 The first two rows of the data series:
0 0.720876
1 -0.765898
dtype: float64
Shell

tail()返回最后n行(观察索引值)。 要显示的元素的默认数量为5,但可以传递自定义数字值。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a series with 4 random numbers
s = pd.Series(np.random.randn(4))
print ("The original series is:")
print s print ("The last two rows of the data series:")
print s.tail(2)
Python

执行上面示例代码,得到以下结果 -

The original series is:
0 -0.655091
1 -0.881407
2 -0.608592
3 -2.341413
dtype: float64 The last two rows of the data series:
2 -0.608592
3 -2.341413
dtype: float64
Shell

DataFrame基本功能

下面来看看数据帧(DataFrame)的基本功能有哪些?下表列出了DataFrame基本功能的重要属性或方法。

编号 属性或方法 描述
1 T 转置行和列。
2 axes 返回一个列,行轴标签和列轴标签作为唯一的成员。
3 dtypes 返回此对象中的数据类型(dtypes)。
4 empty 如果NDFrame完全为空[无项目],则返回为True; 如果任何轴的长度为0。
5 ndim 轴/数组维度大小。
6 shape 返回表示DataFrame的维度的元组。
7 size NDFrame中的元素数。
8 values NDFrame的Numpy表示。
9 head() 返回开头前n行。
10 tail() 返回最后n行。

下面来看看如何创建一个DataFrame并使用上述属性和方法。

示例

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our data series is:")
print df
Python

执行上面示例代码,得到以下结果 -

Our data series is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
Shell

T(转置)示例

返回DataFrame的转置。行和列将交换。参考以下示例代码 -

import pandas as pd
import numpy as np # Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} # Create a DataFrame
df = pd.DataFrame(d)
print ("The transpose of the data series is:")
print df.T
Python

执行上面示例代码,得到以下结果 -

The transpose of the data series is:
0 1 2 3 4 5 6
Age 25 26 25 23 30 29 23
Name Tom James Ricky Vin Steve Minsu Jack
Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
Shell

axes示例

返回行轴标签和列轴标签列表。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Row axis labels and column axis labels are:")
print df.axes
Python

执行上面示例代码,得到以下结果 -

Row axis labels and column axis labels are:

[RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'],
dtype='object')]
Shell

dtypes示例

返回每列的数据类型。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("The data types of each column are:")
print df.dtypes
Python

执行上面示例代码,得到以下结果 -

The data types of each column are:
Age int64
Name object
Rating float64
dtype: object
Shell

empty示例

返回布尔值,表示对象是否为空; 返回True表示对象为空。

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Is the object empty?")
print df.empty
Python

执行上面示例代码,得到以下结果 -

Is the object empty?
False
Shell

ndim示例

返回对象的维数。根据定义,DataFrame是一个2D对象。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The dimension of the object is:")
print df.ndim
Python

执行上面示例代码,得到以下结果 -

Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80 The dimension of the object is:
2
Shell

shape示例

返回表示DataFrame的维度的元组。 元组(a,b),其中a表示行数,b表示列数。

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The shape of the object is:")
print df.shape
Python

执行上面示例代码,得到以下结果 -

Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80 The shape of the object is:
(7, 3)
Shell

size示例

返回DataFrame中的元素数。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The total number of elements in our object is:")
print df.size
Python

执行上面示例代码,得到以下结果 -

Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80 The total number of elements in our object is:
21
Shell

values示例

将DataFrame中的实际数据作为NDarray返回。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our object is:")
print df
print ("The actual data in our data frame is:")
print df.values
Python

执行上面示例代码,得到以下结果 -

Our object is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80
The actual data in our data frame is:
[[25 'Tom' 4.23]
[26 'James' 3.24]
[25 'Ricky' 3.98]
[23 'Vin' 2.56]
[30 'Steve' 3.2]
[29 'Minsu' 4.6]
[23 'Jack' 3.8]]
Shell

head()和tail()示例

要查看DataFrame对象的小样本,可使用head()和tail()方法。head()返回前n行(观察索引值)。显示元素的默认数量为5,但可以传递自定义数字值。参考以下示例代码 -

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our data frame is:")
print df
print ("The first two rows of the data frame is:")
print df.head(2)
Python

执行上面示例代码,得到以下结果 -

Our data frame is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80 The first two rows of the data frame is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
Shell

tail()返回最后n行(观察索引值)。显示元素的默认数量为5,但可以传递自定义数字值。

import pandas as pd
import numpy as np #Create a Dictionary of series
d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Minsu','Jack']),
'Age':pd.Series([25,26,25,23,30,29,23]),
'Rating':pd.Series([4.23,3.24,3.98,2.56,3.20,4.6,3.8])} #Create a DataFrame
df = pd.DataFrame(d)
print ("Our data frame is:")
print df
print ("The last two rows of the data frame is:")
print df.tail(2)
Python

执行上面示例代码,得到以下结果 -

Our data frame is:
Age Name Rating
0 25 Tom 4.23
1 26 James 3.24
2 25 Ricky 3.98
3 23 Vin 2.56
4 30 Steve 3.20
5 29 Minsu 4.60
6 23 Jack 3.80 The last two rows of the data frame is:
Age Name Rating
5 29 Minsu 4.6
6 23 Jack 3.8
Shell
 

Pandas基本功能的更多相关文章

  1. pandas小记:pandas高级功能

    http://blog.csdn.net/pipisorry/article/details/53486777 pandas高级功能:面板数据.字符串方法.分类.可视化. 面板数据 {pandas数据 ...

  2. Pandas基本功能详解

    Pandas基本功能详解 Pandas  Pandas基本功能详解 |轻松玩转Pandas(2) 参考:Pandas基本功能详解 |轻松玩转Pandas(2)

  3. Pandas基本功能之reindex重新索引

    重新索引 reindex重置索引,如果索引值不存在,就引入缺失值 参数介绍 参数 说明 index 用作索引的新序列 method 插值 fill_vlaue 引入缺失值时的替代NaN limit 最 ...

  4. python使用easyinstall安装xlrd、xlwt、pandas等功能模块的方法

    在日常工作中,使用Python时经常要引入一些集成好的第三方功能模块,如读写excel的xlrd和xlwt模块,以及数据分析常用的pandas模块等. 原生的python并不含这些模块,在使用这些功能 ...

  5. Pandas日期功能

    日期功能扩展了时间序列,在财务数据分析中起主要作用.在处理日期数据的同时,我们经常会遇到以下情况 - 生成日期序列 将日期序列转换为不同的频率 创建一个日期范围 通过指定周期和频率,使用date.ra ...

  6. Pandas常用功能

    在使用Pandas之前,需要导入pandas库 import pandas  as pd #pd作为pandas的别名 常用功能如下: 代码 功能1 .DataFrame()   创建一个DataFr ...

  7. Pandas常用功能总结

    1.读取.csv文件 df2 = pd.read_csv('beijingsale.csv', encoding='gb2312',index_col='id',sep='\t',header=Non ...

  8. Pandas基本功能之层次化索引及层次化汇总

    层次化索引 层次化也就是在一个轴上拥有多个索引级别 Series的层次化索引 data=Series(np.random.randn(10),index=[ ['a','a','a','b','b', ...

  9. Pandas基本功能之算术运算、排序和排名

    算术运算和数据对齐 Series和DataFrame中行运算和列运算有种特征叫做广播 在将对象相加时,如果存在不同的索引对,则结果的索引就是该索引对的并集.自动的数据对齐操作在不重叠的索引处引入了NA ...

随机推荐

  1. Refused to execute script from '....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.md

    目录 问题描述 解决过程 总结 问题描述 在整合 Spring Boot.Spring Security.Thymeleaf 的练习中,对页面进行调试时,发现如下错误提示: Refused to ex ...

  2. 巨蟒python全栈开发-第8天 文件操作

    一.文件操作 今日大纲: 1.文件操作->open() open 打开 f=open(文件路径,mode='模式',encoding='编码格式') #python最最底层操作的就是bytes ...

  3. vim_action

    读取文件,显示行号 nl -a.txt brace expansion 花括号扩展 echo a{A{1,2},B{3,4}}b mkdir {2009...2011}-0{1...9} {2009. ...

  4. <2014 04 15> C++语言回顾精要(原创By Andrew)

    C++语言回顾精要 <Visual C++程序设计>张岳新,这本书是很多学校的本科生C++教学用书,今天重新拿来翻了一遍.跟很多国人写的技术书籍一样,写书并不是为了让初学者看懂入门,而是为 ...

  5. centos7 Dockerfile安装nginx

    1.写一个Dockerfile文件 FROM centos MAINTAINER apeng apeng@apenglinux-002.com RUN yum install -y pcre-deve ...

  6. java 统计文件注释个数

    参考:https://segmentfault.com/q/1010000012636380/a-1020000012640905 题目:统计文件中//和/* */注释的个数,双引号中的不算 impo ...

  7. 字符串 (string)与字节数组(byte[])之间的转换

    string str = "abc" //字符串转成编码为GB2312的byte[] byte[] pData =System.Text.Encoding.GetEncoding( ...

  8. 我的Android进阶之旅------>解决Error:Unable to find method 'org.gradle.api.internal.project.ProjectInternal.g

    错误描述 今天在Github上面下载了一份代码,然后导入到Android Studio中直接报了如下图所示的错误: 错误描述如下: Error: Unable to find method 'org. ...

  9. Java String.split() 使用注意

    java的split()方法用于字符串中根据指定的字符进行分割,得到的是一个字符串数组 public String[] split(String regex) Splits this string a ...

  10. (2.2)学习笔记之mysql基础操作(登录及账户权限设置)

    本系列学习笔记主要讲如下几个方面: 本文笔记[三:mysql登录][四:账户权限设置][五:mysql数据库安全配置] 三.mysql登录 常用登录方式如下: 四.账户权限设置 (4.1)查看用户表, ...