在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ Pandas基本功能
Pandas教程
Pandas注意事項&竅門
Pandas IO工具
Pandas重建索引
Pandas稀疏數(shù)據(jù)
Pandas時間差(Timedelta)
Pandas聚合
Pandas字符串和文本數(shù)據(jù)
Pandas分類數(shù)據(jù)
Pandas索引和選擇數(shù)據(jù)
Pandas基本功能
Pandas系列
Pandas數(shù)據(jù)幀(DataFrame)
Pandas日期功能
Pandas缺失數(shù)據(jù)
Pandas與SQL比較
Pandas迭代
Pandas合并/連接
Pandas選項和自定義
Pandas級聯(lián)
Pandas可視化
Pandas數(shù)據(jù)結構
Pandas環(huán)境安裝配置
Pandas統(tǒng)計函數(shù)
Pandas窗口函數(shù)
Pandas面板(Panel)
Pandas排序
Pandas函數(shù)應用
Pandas快速入門
Pandas描述性統(tǒng)計
Pandas分組(GroupBy)

Pandas基本功能

到目前為止,我們了解了三種Pandas數(shù)據(jù)結構以及如何創(chuàng)建它們。接下來將主要關注數(shù)據(jù)幀(DataFrame)對象,因為它在實時數(shù)據(jù)處理中非常重要,并且還討論其他數(shù)據(jù)結構。

系列基本功能

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

現(xiàn)在創(chuàng)建一個系列并演示如何使用上面所有列出的屬性操作。

示例

import pandas as pd
import numpy as np

#Create a series with 100 random numbers
s = pd.Series(np.random.randn(4))
print s

執(zhí)行上面示例代碼,得到以下輸出結果 -

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

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

執(zhí)行上面示例代碼,得到以下輸出結果 -

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

上述結果是從05的值列表的緊湊格式,即:[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

執(zhí)行上面示例代碼,得到以下輸出結果 -

Is the Object empty?
False

ndim示例

返回對象的維數(shù)。根據(jù)定義,一個系列是一個1D數(shù)據(jù)結構,參考以下示例代碼 -

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

執(zhí)行上面示例代碼,得到以下結果 -

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

#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

執(zhí)行上面示例代碼,得到以下結果 -

0   3.078058
1  -1.207803
dtype: float64

The size of the object:
2

values示例

以數(shù)組形式返回系列中的實際數(shù)據(jù)值。

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

執(zhí)行上面示例代碼,得到以下結果 -

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行(觀察索引值)。要顯示的元素的默認數(shù)量為5,但可以傳遞自定義這個數(shù)字值。

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)

執(zhí)行上面示例代碼,得到以下結果 -

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

tail()返回最后n行(觀察索引值)。 要顯示的元素的默認數(shù)量為5,但可以傳遞自定義數(shù)字值。參考以下示例代碼 -

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)

執(zhí)行上面示例代碼,得到以下結果 -

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

DataFrame基本功能

下面來看看數(shù)據(jù)幀(DataFrame)的基本功能有哪些?下表列出了DataFrame基本功能的重要屬性或方法。

編號 屬性或方法 描述
1 T 轉置行和列。
2 axes 返回一個列,行軸標簽和列軸標簽作為唯一的成員。
3 dtypes 返回此對象中的數(shù)據(jù)類型(dtypes)。
4 empty 如果NDFrame完全為空[無項目],則返回為True; 如果任何軸的長度為0。
5 ndim 軸/數(shù)組維度大小。
6 shape 返回表示DataFrame的維度的元組。
7 size NDFrame中的元素數(shù)。
8 values NDFrame的Numpy表示。
9 head() 返回開頭前n行。
10 tail() 返回最后n行。

下面來看看如何創(chuàng)建一個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

執(zhí)行上面示例代碼,得到以下結果 -

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

執(zhí)行上面示例代碼,得到以下結果 -

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

執(zhí)行上面示例代碼,得到以下結果 -

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

返回每列的數(shù)據(jù)類型。參考以下示例代碼 -

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

執(zhí)行上面示例代碼,得到以下結果 -

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

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

執(zhí)行上面示例代碼,得到以下結果 -

Is the object empty?
False

ndim示例

返回對象的維數(shù)。根據(jù)定義,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

執(zhí)行上面示例代碼,得到以下結果 -

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表示行數(shù),b表示列數(shù)。

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

執(zhí)行上面示例代碼,得到以下結果 -

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中的元素數(shù)。參考以下示例代碼 -

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

執(zhí)行上面示例代碼,得到以下結果 -

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中的實際數(shù)據(jù)作為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

執(zhí)行上面示例代碼,得到以下結果 -

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行(觀察索引值)。顯示元素的默認數(shù)量為5,但可以傳遞自定義數(shù)字值。參考以下示例代碼 -

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)

執(zhí)行上面示例代碼,得到以下結果 -

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

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

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)

執(zhí)行上面示例代碼,得到以下結果 -

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