データ選択パンダ

:3頭のメソッドをインデックス化パンダがあり.loc.ilocかつ[]、ノート:.ix0.20.0での使用はしているお勧めしません

import pandas as pd
import numpy as np

[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

[5]アウト:

A B C D
2017年1月1日 0 1 2 3
2017年1月2日 4 5 6 7
2017年1月3日 8 9 10 11
2017年1月4日 12 13 14 15
2017年1月5日 16 17 18 19
2017年1月6日 20 21 22 23

[6]:

一連の列として取得データフレーム

df1["A"]#将dataframe的列获取为一个series

[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

[7]:

df1.A#另一种获取

[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

[8]で:

スライスは、最初の2行を取得します

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

[8]アウト:

A B C D
2017年1月1日 0 1 2 3
2017年1月2日 4 5 6 7

[9]で:

インデックスで行を取得

df1["20170102":"20170104"]#通过索引获取指定行

[9]アウト:

A B C D
2017年1月2日 4 5 6 7
2017年1月3日 8 9 10 11
2017年1月4日 12 13 14 15

[11]で:

ラベルでデータを選択

#通过标签选择数据
df1.loc["20170102"]

[11]アウト:

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

[12]で:

行の指定された列を抽出します

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

[12]アウト:

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

[13]で:

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

[13]アウト:

A B
2017年1月1日 0 1
2017年1月2日 4 5
2017年1月3日 8 9
2017年1月4日 12 13
2017年1月5日 16 17
2017年1月6日 20 21

[14]で:

位置データ選択

#通过位置选择数据
df1.iloc[2]#提取第二行

[14]アウト:

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

[15]で:

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

[15]アウト:

C D
2017年1月2日 6 7
2017年1月3日 10 11

[18]で:

不連続な行と列の抽出

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

[18]アウト:

B D
2017年1月2日 5 7
2017年1月3日 9 11
2017年1月5日 17 19

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

[20]アウト:

A C
2017年1月3日 8 10
2017年1月4日 12 14

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

[23]アウト:

C D
2017年1月2日 6 7
2017年1月3日 10 11
2017年1月4日 14 15

[24]で:

行サイズ決意値

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

[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

[25]で:

df1[df1.A>6]#根据判断组成新的DataFrame

[25]アウト:

A B C D
2017年1月3日 8 9 10 11
2017年1月4日 12 13 14 15
2017年1月5日 16 17 18 19
2017年1月6日 20 21 22 23

[]内:

 

おすすめ

転載: www.cnblogs.com/mrwuzs/p/11325021.html