到目前为止,我们了解了三种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
创建一个包含100个随机数的 Series
s = pd.Series(np.random.randn(4))
print(s)
0 1.549542 1 -0.135235 2 -0.471869 3 -0.050726 dtype: float64
返回系列的标签列表。参考以下示例代码:
import pandas as pd
import numpy as np
创建一个包含100个随机数的 Series
s = pd.Series(np.random.randn(4))
print ("The axes are:")
The axes are:
print(s.axes)
[RangeIndex(start=0, stop=4, step=1)]
上述结果是从0到5的值列表的紧凑格式,即:[0,1,2,3,4]
。
s = pd.Series(np.random.randn(4))
print("Is the Object empty?", s.empty)
Is the Object empty? False
s = pd.Series(np.random.randn(4))
print(s)
0 -1.688740 1 -0.427721 2 0.685258 3 -0.192390 dtype: float64
print("The dimensions of the object:", s.ndim)
The dimensions of the object: 1
s = pd.Series(np.random.randn(2))
print(s)
0 -1.052340 1 0.528544 dtype: float64
print("The size of the object:", s.size)
The size of the object: 2
s = pd.Series(np.random.randn(4))
print(s)
0 0.390511 1 -0.933345 2 -0.604551 3 0.039096 dtype: float64
print("The actual data series is:", s.values)
The actual data series is: [ 0.39051077 -0.93334486 -0.60455064 0.03909632]
要查看 Series
或 DataFrame
对象的小样本,请使用 head()
和 tail()
方法。
head()
返回前 n
行(观察索引值)。要显示的元素的默认数量为5,但可以传递自定义这个数字值。
创建一个包含4个随机数的序列
s = pd.Series(np.random.randn(4))
print("The original series is:")
The original series is:
print(s)
0 0.874249 1 0.275182 2 0.661165 3 -1.070576 dtype: float64
print("The first two rows of the data series:")
The first two rows of the data series:
print(s.head(2))
0 0.874249 1 0.275182 dtype: float64
tail()
返回最后n行(观察索引值)。 要显示的元素的默认数量为 5
,但可以传递自定义数字值。参考以下示例代码:
创建一个包含4个随机数的 Series
。
s = pd.Series(np.random.randn(4))
print("The original series is:")
The original series is:
print(s)
0 0.243765 1 -0.323074 2 0.238040 3 0.082338 dtype: float64
print("The last two rows of the data series:")
The last two rows of the data series:
print(s.tail(2))
2 0.238040 3 0.082338 dtype: float64
执行上面示例代码,得到上面打印的结果。
编号 | 属性或方法 | 描述 |
---|---|---|
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并使用上述属性和方法。
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print("Our data series is:")
Our data series is:
print(df)
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
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("The transpose of the data series is:")
The transpose of the data series is:
print (df.T)
0 1 2 3 4 5 6 Name Tom James Ricky Vin Steve Minsu Jack Age 25 26 25 23 30 29 23 Rating 4.23 3.24 3.98 2.56 3.2 4.6 3.8
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Row axis labels and column axis labels are:")
Row axis labels and column axis labels are:
print( df.axes)
[RangeIndex(start=0, stop=7, step=1), Index(['Name', 'Age', 'Rating'], dtype='object')]
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print("The data types of each column are:")
The data types of each column are:
print(df.dtypes)
Name object Age int64 Rating float64 dtype: object
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Is the object empty?")
Is the object empty?
print( df.empty)
False
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Our object is:")
Our object is:
print (df)
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
print ("The dimension of the object is:")
The dimension of the object is:
print (df.ndim)
2
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Our object is:")
Our object is:
print (df)
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
print ("The shape of the object is:")
The shape of the object is:
print (df.shape)
(7, 3)
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Our object is:")
Our object is:
print (df)
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
print ("The total number of elements in our object is:")
The total number of elements in our object is:
print (df.size)
21
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Our object is:")
Our object is:
print (df)
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
print ("The actual data in our data frame is:")
The actual data in our data frame is:
print (df.values)
[['Tom' 25 4.23] ['James' 26 3.24] ['Ricky' 25 3.98] ['Vin' 23 2.56] ['Steve' 30 3.2] ['Minsu' 29 4.6] ['Jack' 23 3.8]]
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Our data frame is:")
Our data frame is:
print (df)
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
print ("The first two rows of the data frame is:")
The first two rows of the data frame is:
print (df.head(2))
Name Age Rating 0 Tom 25 4.23 1 James 26 3.24
tail()
返回最后 n
行(观察索引值)。显示元素的默认数量为5,但可以传递自定义数字值。
import pandas as pd
import numpy as np
创建一个由 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])}
创建一个DataFrame(数据帧):
df = pd.DataFrame(d)
print ("Our data frame is:")
Our data frame is:
print (df)
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
print ("The last two rows of the data frame is:")
The last two rows of the data frame is:
print (df.tail(2))
Name Age Rating 5 Minsu 29 4.6 6 Jack 23 3.8