Pandas基本属性和方法
时间: 2020-03-28来源:OSCHINA
前景提要
Series基本功能: axes 返回行轴标签列表。 dtype 返回对象的数据类型(dtype)。 empty 如果系列为空,则返回True。 ndim 返回底层数据的维数,默认定义:1。 size 返回基础数据中的元素数。 values 将系列作为ndarray返回。 head() 返回前n行。 tail() 返回最后n行。
DataFrame基本功能 T 转置行和列。 axes 返回一个列,行轴标签和列轴标签作为唯一的成员。 dtypes 返回此对象中的数据类型(dtypes)。 empty 如果NDFrame完全为空[无项目],则返回为True; 如果任何轴的长度为0。 ndim 轴/数组维度大小。 shape 返回表示DataFrame的维度的元组。 size NDFrame中的元素数。 values NDFrame的Numpy表示。 head()返回开头前n行。 tail()返回最后n行。
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
执行上面示例代码,得到以下结果 - 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
axes
返回行轴标签和列轴标签列表
实例1: #Create a series with 100 random numbers s = pd.Series(np.random.randn(4)) print ("The axes are:") print s.axes
执行上面示例代码,得到以下输出结果 - The axes are: [RangeIndex(start=0, stop=4, step=1)]
实例2: #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
执行上面示例代码,得到以下结果 - Row axis labels and column axis labels are: [RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'], dtype='object')]
dtypes
返回每列的数据类型 #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
执行上面示例代码,得到以下结果 - The data types of each column are: Age int64 Name object Rating float64 dtype: object
empty
返回布尔值,表示对象是否为空; 返回True表示对象为空。 #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
执行上面示例代码,得到以下结果 - Is the object empty? False
ndim
返回对象的维数。 #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
执行上面示例代码,得到以下结果 - 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
shape
其中a表示行数,b表示列数。 #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
执行上面示例代码,得到以下结果 - 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)
size
返回元素数。 #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
执行上面示例代码,得到以下结果 - 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
values
以数组形式返回实际数据值。
实例1:
以数组形式返回系列中的实际数据值。 #Create a series with 4 random numbers s = pd.Series(np.random.randn(4)) print ("The actual data series is:") print s.values
执行上面示例代码,得到以下结果 - The actual data series is: [ 1.78737302 -0.60515881 0.18047664 -0.1409218 ]
实例2:
将DataFrame中的实际数据作为NDarray返回 #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 actual data in our data frame is:") print df.values
执行上面示例代码,得到以下结果 - 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]]
head()和tail()
要查看DataFrame对象的小样本,可使用head()和tail()方法。head()返回前n行(观察索引值)。显示元素的默认数量为5,但可以传递自定义数字值。tail()返回最后n行(观察索引值)。显示元素的默认数量为5.
实例: #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)) print ("The last two rows of the data frame is:") print (df.tail(2))
输出: Our data frame is: Name Age Rating 0 Tom 25 4.23 1 James 26 3.24 2 Ricky 25 3.98 3 Vin 23 2.56 4 Steve 30 3.20 5 Minsu 29 4.60 6 Jack 23 3.80 The first two rows of the data frame is: Name Age Rating 0 Tom 25 4.23 1 James 26 3.24 The last two rows of the data frame is: Name Age Rating 5 Minsu 29 4.6 6 Jack 23 3.8

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行