pandas之數據選擇


pandas中有三種索引方法:.loc.iloc[],注意:.ix的用法在0.20.0中已經不建議使用

import pandas as pd
import numpy as np

In [5]:

dates = pd.date_range("20170101",periods=6)
df1 = pd.DataFrame(np.arange(24).reshape(6,4),index=dates,columns=["A","B","C","D"])
df1

Out[5]:

A B C D
2017-01-01 0 1 2 3
2017-01-02 4 5 6 7
2017-01-03 8 9 10 11
2017-01-04 12 13 14 15
2017-01-05 16 17 18 19
2017-01-06 20 21 22 23

In [6]:

將dataframe的列獲取為一個series

df1["A"]#將dataframe的列獲取為一個series

Out[6]:

2017-01-01     0
2017-01-02     4
2017-01-03     8
2017-01-04    12
2017-01-05    16
2017-01-06    20
Freq: D, Name: A, dtype: int32

In [7]:

df1.A#另一種獲取

Out[7]:

2017-01-01     0
2017-01-02     4
2017-01-03     8
2017-01-04    12
2017-01-05    16
2017-01-06    20
Freq: D, Name: A, dtype: int32

In [8]:

切片,獲取前2行

df1[0:2]#切片,獲取前2行

Out[8]:

A B C D
2017-01-01 0 1 2 3
2017-01-02 4 5 6 7

In [9]:

通過索引獲取指定行

df1["20170102":"20170104"]#通過索引獲取指定行

Out[9]:

A B C D
2017-01-02 4 5 6 7
2017-01-03 8 9 10 11
2017-01-04 12 13 14 15

In [11]:

通過標簽選擇數據

#通過標簽選擇數據
df1.loc["20170102"]

Out[11]:

A    4
B    5
C    6
D    7
Name: 2017-01-02 00:00:00, dtype: int32

In [12]:

提取某個行的指定列

df1.loc["20170102",["A","C"]]#提取某個行的指定列

Out[12]:

A    4
C    6
Name: 2017-01-02 00:00:00, dtype: int32

In [13]:

df1.loc[:,["A","B"]]

Out[13]:

A B
2017-01-01 0 1
2017-01-02 4 5
2017-01-03 8 9
2017-01-04 12 13
2017-01-05 16 17
2017-01-06 20 21

In [14]:

通過位置選擇數據

#通過位置選擇數據
df1.iloc[2]#提取第二行

Out[14]:

A     8
B     9
C    10
D    11
Name: 2017-01-03 00:00:00, dtype: int32

In [15]:

df1.iloc[1:3,2:4]

Out[15]:

C D
2017-01-02 6 7
2017-01-03 10 11

In [18]:

提取不連續的行和列

#提取不連續的行和列
df1.iloc[[1,2,4],[1,3]]

Out[18]:

B D
2017-01-02 5 7
2017-01-03 9 11
2017-01-05 17 19

In [20]:

#混合標簽位置選擇
df1.ix[2:4,["A","C"]]
c:\users\wuzs\appdata\local\programs\python\python36-32\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  
c:\users\wuzs\appdata\local\programs\python\python36-32\lib\site-packages\pandas\core\indexing.py:808: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  retval = getattr(retval, self.name)._getitem_axis(key, axis=i)

Out[20]:

A C
2017-01-03 8 10
2017-01-04 12 14

In [23]:

df1.ix["20170102":"20170104",2:4]
c:\users\wuzs\appdata\local\programs\python\python36-32\lib\site-packages\ipykernel_launcher.py:1: FutureWarning: 
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing

See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#ix-indexer-is-deprecated
  """Entry point for launching an IPython kernel.

Out[23]:

C D
2017-01-02 6 7
2017-01-03 10 11
2017-01-04 14 15

In [24]:

判斷某一行的值大小

#判斷某一行的值大小
df1.A >6

Out[24]:

2017-01-01    False
2017-01-02    False
2017-01-03     True
2017-01-04     True
2017-01-05     True
2017-01-06     True
Freq: D, Name: A, dtype: bool

In [25]:

df1[df1.A>6]#根據判斷組成新的DataFrame

Out[25]:

A B C D
2017-01-03 8 9 10 11
2017-01-04 12 13 14 15
2017-01-05 16 17 18 19
2017-01-06 20 21 22 23

In [ ]:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM