Pandas 基本方法

Pandas 基本方法實例

到目前為止,我們了解了三個Pandas DataStructures以及如何創(chuàng)建它們。由于它在實時數(shù)據(jù)處理中的重要性,因此我們將主要關(guān)注DataFrame對象,并討論其他一些DataStructures。

方法描述
axes返回行軸標(biāo)簽的列表
dtype返回對象的dtype。
empty如果Series為空,則返回True。
ndim根據(jù)定義返回基礎(chǔ)數(shù)據(jù)的維數(shù)。
size返回基礎(chǔ)數(shù)據(jù)中的元素數(shù)。
values將Series返回為ndarray。
head()返回前n行。
tail()返回最后n行。
接下來我們創(chuàng)建一個Series,并看看上所有列表的屬性操作。
 import pandas as pd
 import numpy as np
 # 用100隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print(s)

運(yùn)行結(jié)果:

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

axes

返回Series標(biāo)簽的列表

 import pandas as pd
 import numpy as np
 # 用100隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print ("The axes are:")
 print(s.axes)

運(yùn)行結(jié)果:

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

以上結(jié)果是0到5(即[0,1,2,3,4])。

empty

返回布爾值,說明對象是否為空。True表示對象為空

 import pandas as pd
 import numpy as np
 # 用100隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print ("Is the Object empty?")
 print(s.empty)

運(yùn)行結(jié)果:

Is the Object empty?
False

ndim

返回對象的維數(shù)。根據(jù)定義,Series 是一個1D 數(shù)據(jù)結(jié)構(gòu),所以它返回

 import pandas as pd
 import numpy as np
 # 用4個隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print s
 print ("The dimensions of the object:")
 print(s.ndim)

運(yùn)行結(jié)果:

     0   0.175898
1   0.166197
2  -0.609712
3  -1.377000
dtype: float64

The dimensions of the object:
1

size

返回Series的大小(長度).

 import pandas as pd
 import numpy as np
 # 用4個隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(2))
 print s
 print ("The size of the object:")
 print(s.size)

運(yùn)行結(jié)果:

0   3.078058
1  -1.207803
dtype: float64

The size of the object:
2

values

以數(shù)組形式返回Series數(shù)據(jù)

 import pandas as pd
 import numpy as np
 # 用4個隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print s
 print ("The actual data series is:")
 print(s.values)

運(yùn)行結(jié)果:

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對象的頭尾數(shù)據(jù),請使用head() 和tail() 方法。

head() 返回前n行(觀察索引值)。默認(rèn)顯示的元素數(shù)是5,但是您可以傳遞自定義數(shù)字。

 import pandas as pd
 import numpy as np
 # 用4個隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print ("最初的系列是:")
 print s
 print ("數(shù)據(jù)系列的前兩行:")
 print(s.head(2))

運(yùn)行結(jié)果:

最初的系列是:
0   0.720876
1  -0.765898
2   0.479221
3  -0.139547
dtype: float64

數(shù)據(jù)系列的前兩行:
0   0.720876
1  -0.765898
dtype: float64

tail() 返回最后n行(觀察索引值)。默認(rèn)顯示的元素數(shù)是5,但是您可以傳遞自定義數(shù)字。

 import pandas as pd
 import numpy as np
 # 用4個隨機(jī)數(shù)創(chuàng)建一個Series
 s = pd.Series(np.random.randn(4))
 print("最初的系列是:")
 print(s)
 print("數(shù)據(jù)序列的最后兩行:")
 print(s)tail(2)

運(yùn)行結(jié)果:

最初的系列是:
0 -0.655091
1 -0.881407
2 -0.608592
3 -2.341413
dtype: float64

數(shù)據(jù)序列的最后兩行:
2 -0.608592
3 -2.341413
dtype: float64

DataFrame 基本功能

現(xiàn)在讓我們了解什么是DataFrame基本功能。下表列出了有助于DataFrame基本功能的重要屬性或方法。

屬性/方法描述
T行和列互相轉(zhuǎn)換
axes返回以行軸標(biāo)簽和列軸標(biāo)簽為唯一成員的列表。
dtypes返回此對象中的dtypes。
empty如果NDFrame完全為空[沒有項目],則為true;否則為false。如果任何軸的長度為0。
ndim軸數(shù)/數(shù)組尺寸。
shape返回表示DataFrame維度的元組。
sizeNDFrame中的元素數(shù)。
valuesNDFrame的數(shù)字表示。
head()返回前n行。
tail()返回最后n行。

下面我們下創(chuàng)建一個DataFrame并查看上述屬性的所有操作方式。

Example

 import pandas as pd
 import numpy as np
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("Our data series is:")
 print(df)

運(yùn)行結(jié)果:

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    Smith   4.60
6   23    Jack    3.80

T (Transpose)

返回DataFrame的轉(zhuǎn)置。行和列將互換。

 import pandas as pd
 import numpy as np
  
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("數(shù)據(jù)序列的轉(zhuǎn)置是:")
 print(df.T)

運(yùn)行結(jié)果:

數(shù)據(jù)序列的轉(zhuǎn)置是:
         0     1       2      3      4      5       6
Age      25    26      25     23     30     29      23
Name     Tom   James   Ricky  Vin    Steve  Smith   Jack
Rating   4.23  3.24    3.98   2.56   3.2    4.6     3.8

axes

返回行軸標(biāo)簽和列軸標(biāo)簽的列表。

 import pandas as pd
 import numpy as np
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("行軸標(biāo)簽和列軸標(biāo)簽是:")
 print(df.axes)

運(yùn)行結(jié)果:

  行軸標(biāo)簽和列軸標(biāo)簽是:
 [RangeIndex(start=0, stop=7, step=1), Index([u'Age', u'Name', u'Rating'],
 dtype='object')]

dtypes

返回每一列的數(shù)據(jù)類型。

 import pandas as pd
 import numpy as np
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("每列的數(shù)據(jù)類型如下:")
 print(df.dtypes)

運(yùn)行結(jié)果:

每列的數(shù)據(jù)類型如下:
Age     int64
Name    object
Rating  float64
dtype: object

empty

返回布爾值,說明對象是否為空;True表示對象為空。

 import pandas as pd
 import numpy as np
  
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
  
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("Is the object empty?")
 print(df.empty)

運(yùn)行結(jié)果:

 Is the object empty?
 False

ndim

返回對象的數(shù)量。根據(jù)定義,DataFrame是2D對象。

 import pandas as pd
 import numpy as np
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("Our object is:")
 print df
 print ("The dimension of the object is:")
 print(df.ndim)

運(yùn)行結(jié)果:

     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     Smith    4.60
6     23     Jack     3.80

The dimension of the object is:
2

shape

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

 import pandas as pd
 import numpy as np
  
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
  
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("Our object is:")
 print df
 print ("The shape of the object is:")
 print(df.shape)

運(yùn)行結(jié)果:

     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    Smith   4.60
6  23    Jack    3.80

The shape of the object is:
(7, 3)

size

返回DataFrame中的元素數(shù)。

 import pandas as pd
 import numpy as np
  
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
  
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("Our object is:")
 print df
 print ("The total number of elements in our object is:")
 print(df.size)

運(yùn)行結(jié)果:

     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    Smith   4.60
6   23    Jack    3.80

The total number of elements in our object is:
21

values

以NDarray的形式返回DataFrame中的實際數(shù)據(jù)。

 import pandas as pd
 import numpy as np
  
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
  
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("Our object is:")
 print df
 print ("The actual data in our data frame is:")
 print(df.values)

運(yùn)行結(jié)果:

     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    Smith   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 'Smith' 4.6]
[23 'Jack' 3.8]]

Head & Tail

要查看DataFrame對象的頭尾數(shù)據(jù),請使用head()和tail()方法。head() 返回前n行(觀察索引值)。默認(rèn)顯示的元素數(shù)是5,但是您可以傳遞自定義數(shù)字。

 import pandas as pd
 import numpy as np
  
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
 # 創(chuàng)建一個 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))

運(yùn)行結(jié)果:

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

tail() 返回最后n行(觀察索引值)。默認(rèn)顯示的元素數(shù)是5,但是您可以傳遞自定義數(shù)字。

 import pandas as pd
 import numpy as np
 # 創(chuàng)建Series字典
 d = {'Name':pd.Series(['Tom','James','Ricky','Vin','Steve','Smith','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])}
  
 # 創(chuàng)建一個 DataFrame
 df = pd.DataFrame(d)
 print ("我們的數(shù)據(jù)幀是:")
 print df
 print ("數(shù)據(jù)幀的最后兩行是:")
 print(df.tail(2))

運(yùn)行結(jié)果:

我們的數(shù)據(jù)幀是:
    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    Smith   4.60
6   23    Jack    3.80

數(shù)據(jù)幀的最后兩行是:
    Age   Name    Rating
5   29    Smith    4.6
6   23    Jack     3.8
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清