Pandas | 05 基本功能

到目前爲止,咱們瞭解了三種Pandas數據結構以及如何建立它們。接下來將主要關注數據幀(DataFrame)對象,由於它在實時數據處理中很是重要,而且還討論其餘數據結構。python

 

1、系列基本功能

編號 屬性或方法 描述
1 axes 返回行軸標籤列表。
2 dtype 返回對象的數據類型(dtype)。
3 empty 若是系列爲空,則返回True
4 ndim 返回底層數據的維數,默認定義:1
5 size 返回基礎數據中的元素數。
6 values 將系列做爲ndarray返回。
7 head() 返回前n行。
8 tail() 返回最後n行。

如今建立一個系列並演示如何使用上面全部列出的屬性操做。shell

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(4))
print(s)

輸出結果:數組

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

axes示例數據結構

import pandas as pd
import numpy as np


s = pd.Series(np.random.randn(4))
print ("The axes are:")
print(s.axes)

輸出結果:dom

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

empty示例spa

返回布爾值,表示對象是否爲空。返回True則表示對象爲空。code

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(4))
print ("Is the Object empty?")
print(s.empty)
輸出結果:
Is the Object empty? False 
 

ndim示例對象

返回對象的維數。根據定義,一個系列是一個1D數據結構,參考如下示例代碼 blog

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(4))
print(s)
print('\n')

print ("The dimensions of the object:",s.ndim)

輸出結果 -索引

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

size示例

返回系列的大小(長度)。參考如下示例代碼

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(2))
print(s)
print('\n')

print ("The size of the object:",s.size)

輸出結果:

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

values示例

數組形式返回系列中的實際數據值。

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(4))
print(s)
print('\n')

print ("The actual data series is:",s.values)

輸出結果:

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 ]
 

head()和tail()方法示例

要查看Series或DataFrame對象的小樣本,請使用head()tail()方法。

head()返回前n行(觀察索引值)。要顯示的元素的默認數量爲5,但能夠傳遞自定義這個數字值。

tail()返回最後n行(觀察索引值)。 要顯示的元素的默認數量爲5,但能夠傳遞自定義數字值。

import pandas as pd
import numpy as np

s = pd.Series(np.random.randn(4))
print ("The original series is:")
print(s)
print('\n')

print ("The first two rows of the data series:")
print(s.head(2))
print('\n')

print ("The last two rows of the data series:")
print(s.tail(2))
輸出結果:
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.765898dtype: float64
The last two rows of the data series:
2 0.479221
3 -0.139547
dtype: float64
 

2、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

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])}


df = pd.DataFrame(d)
print ("Our data series is:")
print(df) 

輸出結果:

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
 

T(轉置)示例

返回DataFrame的轉置。行和列將交換。

import pandas as pd

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])}


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示例

返回行軸標籤和列軸標籤列表。

import pandas as pd


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])}

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示例

返回每列的數據類型。

import pandas as pd

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])}

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表示對象爲空。

import pandas as pd

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])}

df = pd.DataFrame(d)
print ("Is the object empty?",df.empty)

輸出結果:

Is the object empty? False
 

ndim示例

返回對象的維數。根據定義,DataFrame是一個2D對象。

import pandas as pd

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])}

df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n')

print ("The dimension of the object is:",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示例

返回表示DataFrame的維度的元組。 元組(a,b),其中a表示行數,b表示列數。

import pandas as pd

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])}

df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n')

print ("The shape of the object is:",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示例

返回DataFrame中的元素數。

import pandas as pd

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])}


df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n')

print ("The total number of elements in our object is:",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示例

DataFrame中的實際數據做爲ndarray返回。

import pandas as pd


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])}


df = pd.DataFrame(d)
print ("Our object is:")
print(df)
print('\n')
print ("The actual data in our data frame is:") print(df.values)

輸出結果:

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]]
 

head()和tail()示例

要查看DataFrame對象的小樣本,可以使用head()tail()方法。

head()返回前n行(觀察索引值)。顯示元素的默認數量爲5,但能夠傳遞自定義數字值。

tail()返回最後n行(觀察索引值)。顯示元素的默認數量爲5,但能夠傳遞自定義數字值。

import pandas as pd

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])}

df = pd.DataFrame(d)
print ("Our data frame is:")
print(df)
print('\n')

print ("The first two rows of the data frame is:")
print(df.head(2))
print('\n')

print ("The last two rows of the data frame is:")
print(df.tail(2))

輸出結果:

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
The last two rows of the data frame is: Age Name Rating 5 29 Minsu 4.6 6 23 Jack 3.8
相關文章
相關標籤/搜索