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

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

Pandas數(shù)據(jù)幀(DataFrame)

數(shù)據(jù)幀(DataFrame)是二維數(shù)據(jù)結(jié)構(gòu),即數(shù)據(jù)以行和列的表格方式排列。

數(shù)據(jù)幀(DataFrame)的功能特點(diǎn):

  • 潛在的列是不同的類(lèi)型
  • 大小可變
  • 標(biāo)記軸(行和列)
  • 可以對(duì)行和列執(zhí)行算術(shù)運(yùn)算

結(jié)構(gòu)體

假設(shè)要?jiǎng)?chuàng)建一個(gè)包含學(xué)生數(shù)據(jù)的數(shù)據(jù)幀。參考以下圖示 -

可以將上圖表視為SQL表或電子表格數(shù)據(jù)表示。

pandas.DataFrame

pandas中的DataFrame可以使用以下構(gòu)造函數(shù)創(chuàng)建 -

pandas.DataFrame( data, index, columns, dtype, copy)

構(gòu)造函數(shù)的參數(shù)如下 -

編號(hào) 參數(shù) 描述
1 data 數(shù)據(jù)采取各種形式,如:ndarray,series,map,lists,dictconstant和另一個(gè)DataFrame。
2 index 對(duì)于行標(biāo)簽,要用于結(jié)果幀的索引是可選缺省值np.arrange(n),如果沒(méi)有傳遞索引值。
3 columns 對(duì)于列標(biāo)簽,可選的默認(rèn)語(yǔ)法是 - np.arange(n)。 這只有在沒(méi)有索引傳遞的情況下才是這樣。
4 dtype 每列的數(shù)據(jù)類(lèi)型。
5 copy 如果默認(rèn)值為False,則此命令(或任何它)用于復(fù)制數(shù)據(jù)。

創(chuàng)建DataFrame

Pandas數(shù)據(jù)幀(DataFrame)可以使用各種輸入創(chuàng)建,如 -

  • 列表
  • 字典
  • 系列
  • Numpy ndarrays
  • 另一個(gè)數(shù)據(jù)幀(DataFrame)

在本章的后續(xù)章節(jié)中,我們將看到如何使用這些輸入創(chuàng)建數(shù)據(jù)幀(DataFrame)。

創(chuàng)建一個(gè)空的DataFrame

創(chuàng)建基本數(shù)據(jù)幀是空數(shù)據(jù)幀。
示例

#import the pandas library and aliasing as pd
import pandas as pd
df = pd.DataFrame()
print df

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

Empty DataFrame
Columns: []
Index: []

從列表創(chuàng)建DataFrame

可以使用單個(gè)列表或列表列表創(chuàng)建數(shù)據(jù)幀(DataFrame)。

實(shí)例-1

import pandas as pd
data = [1,2,3,4,5]
df = pd.DataFrame(data)
print df

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

     0
0    1
1    2
2    3
3    4
4    5

實(shí)例-2

import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print df

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

      Name      Age
0     Alex      10
1     Bob       12
2     Clarke    13

實(shí)例-3

import pandas as pd
data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'],dtype=float)
print df

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

      Name     Age
0     Alex     10.0
1     Bob      12.0
2     Clarke   13.0

注意 - 可以觀(guān)察到,dtype參數(shù)將Age列的類(lèi)型更改為浮點(diǎn)。

從ndarrays/Lists的字典來(lái)創(chuàng)建DataFrame

所有的ndarrays必須具有相同的長(zhǎng)度。如果傳遞了索引(index),則索引的長(zhǎng)度應(yīng)等于數(shù)組的長(zhǎng)度。

如果沒(méi)有傳遞索引,則默認(rèn)情況下,索引將為range(n),其中n為數(shù)組長(zhǎng)度。

實(shí)例-1

import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data)
print df

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

      Age      Name
0     28        Tom
1     34       Jack
2     29      Steve
3     42      Ricky

注 - 觀(guān)察值0,1,2,3。它們是分配給每個(gè)使用函數(shù)range(n)的默認(rèn)索引。

示例-2

使用數(shù)組創(chuàng)建一個(gè)索引的數(shù)據(jù)幀(DataFrame)。

import pandas as pd
data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
df = pd.DataFrame(data, index=['rank1','rank2','rank3','rank4'])
print df

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

         Age    Name
rank1    28      Tom
rank2    34     Jack
rank3    29    Steve
rank4    42    Ricky

注意 - index參數(shù)為每行分配一個(gè)索引。

從列表創(chuàng)建數(shù)據(jù)幀DataFrame

字典列表可作為輸入數(shù)據(jù)傳遞以用來(lái)創(chuàng)建數(shù)據(jù)幀(DataFrame),字典鍵默認(rèn)為列名。

實(shí)例-1

以下示例顯示如何通過(guò)傳遞字典列表來(lái)創(chuàng)建數(shù)據(jù)幀(DataFrame)。

import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data)
print df

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

    a    b      c
0   1   2     NaN
1   5   10   20.0

注意 - 觀(guān)察到,NaN(不是數(shù)字)被附加在缺失的區(qū)域。

示例-2

以下示例顯示如何通過(guò)傳遞字典列表和行索引來(lái)創(chuàng)建數(shù)據(jù)幀(DataFrame)。

import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]
df = pd.DataFrame(data, index=['first', 'second'])
print df

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

        a   b       c
first   1   2     NaN
second  5   10   20.0

實(shí)例-3

以下示例顯示如何使用字典,行索引和列索引列表創(chuàng)建數(shù)據(jù)幀(DataFrame)。

import pandas as pd
data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]

#With two column indices, values same as dictionary keys
df1 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b'])

#With two column indices with one index with other name
df2 = pd.DataFrame(data, index=['first', 'second'], columns=['a', 'b1'])
print df1
print df2

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

#df1 output
         a  b
first    1  2
second   5  10

#df2 output
         a  b1
first    1  NaN
second   5  NaN

注意 - 觀(guān)察,df2使用字典鍵以外的列索引創(chuàng)建DataFrame; 因此,附加了NaN到位置上。 而df1是使用列索引創(chuàng)建的,與字典鍵相同,所以也附加了NaN。

從系列的字典來(lái)創(chuàng)建DataFrame

字典的系列可以傳遞以形成一個(gè)DataFrame。 所得到的索引是通過(guò)的所有系列索引的并集。

示例

import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
      'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print df
`

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

      one    two
a     1.0    1
b     2.0    2
c     3.0    3
d     NaN    4

注意 - 對(duì)于第一個(gè)系列,觀(guān)察到?jīng)]有傳遞標(biāo)簽'd',但在結(jié)果中,對(duì)于d標(biāo)簽,附加了NaN。

現(xiàn)在通過(guò)實(shí)例來(lái)了解列選擇,添加和刪除。

列選擇

下面將通過(guò)從數(shù)據(jù)幀(DataFrame)中選擇一列。

示例

import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
      'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print df ['one']

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

a     1.0
b     2.0
c     3.0
d     NaN
Name: one, dtype: float64

列添加

下面將通過(guò)向現(xiàn)有數(shù)據(jù)框添加一個(gè)新列來(lái)理解這一點(diǎn)。

示例

import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
      'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

# Adding a new column to an existing DataFrame object with column label by passing new series

print ("Adding a new column by passing as Series:")
df['three']=pd.Series([10,20,30],index=['a','b','c'])
print df

print ("Adding a new column using the existing columns in DataFrame:")
df['four']=df['one']+df['three']

print df

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

Adding a new column by passing as Series:
     one   two   three
a    1.0    1    10.0
b    2.0    2    20.0
c    3.0    3    30.0
d    NaN    4    NaN

Adding a new column using the existing columns in DataFrame:
      one   two   three    four
a     1.0    1    10.0     11.0
b     2.0    2    20.0     22.0
c     3.0    3    30.0     33.0
d     NaN    4     NaN     NaN

列刪除

列可以刪除或彈出; 看看下面的例子來(lái)了解一下。

例子

# Using the previous DataFrame, we will delete a column
# using del function
import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 
     'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']), 
     'three' : pd.Series([10,20,30], index=['a','b','c'])}

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

# using del function
print ("Deleting the first column using DEL function:")
del df['one']
print df

# using pop function
print ("Deleting another column using POP function:")
df.pop('two')
print df

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

Our dataframe is:
      one   three  two
a     1.0    10.0   1
b     2.0    20.0   2
c     3.0    30.0   3
d     NaN     NaN   4

Deleting the first column using DEL function:
      three    two
a     10.0     1
b     20.0     2
c     30.0     3
d     NaN      4

Deleting another column using POP function:
   three
a  10.0
b  20.0
c  30.0
d  NaN

行選擇,添加和刪除

現(xiàn)在將通過(guò)下面實(shí)例來(lái)了解行選擇,添加和刪除。我們從選擇的概念開(kāi)始。

標(biāo)簽選擇

可以通過(guò)將行標(biāo)簽傳遞給loc()函數(shù)來(lái)選擇行。參考以下示例代碼 -

import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 
     'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print df.loc['b']

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

one 2.0
two 2.0
Name: b, dtype: float64

結(jié)果是一系列標(biāo)簽作為DataFrame的列名稱(chēng)。 而且,系列的名稱(chēng)是檢索的標(biāo)簽。

按整數(shù)位置選擇

可以通過(guò)將整數(shù)位置傳遞給iloc()函數(shù)來(lái)選擇行。參考以下示例代碼 -

import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
     'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print df.iloc[2]

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

one   3.0
two   3.0
Name: c, dtype: float64

行切片

可以使用:運(yùn)算符選擇多行。參考以下示例代碼 -

import pandas as pd

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 
    'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print df[2:4]

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

      one    two
c     3.0     3
d     NaN     4

附加行

使用append()函數(shù)將新行添加到DataFrame。 此功能將附加行結(jié)束。

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)
print df

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

   a  b
0  1  2
1  3  4
0  5  6
1  7  8

刪除行

使用索引標(biāo)簽從DataFrame中刪除或刪除行。 如果標(biāo)簽重復(fù),則會(huì)刪除多行。

如果有注意,在上述示例中,有標(biāo)簽是重復(fù)的。這里再多放一個(gè)標(biāo)簽,看看有多少行被刪除。

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)

# Drop rows with label 0
df = df.drop(0)

print df

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

  a b
1 3 4
1 7 8

在上面的例子中,一共有兩行被刪除,因?yàn)檫@兩行包含相同的標(biāo)簽0