0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pandas.DataFrameに対する取得位置と取得タイプの違い

Last updated at Posted at 2021-03-03

Pandas.DataFrameに対する取得位置と取得タイプの違い

Pandas.DataFrame.ilocを使って、データの取得と代入、関数呼び出しを行って、添字の違いによるデータの型の違いなどを、Jupyter上で調べてみました。

iloc

取得

# データフレームを作る
import pandas as pd

df = pd.DataFrame( [[1,2,3,4,5],[10,20,30,40,50],[100,200,300,400,500]] )
print(df)
print(type(df))
print(hex(id(df)))
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
<class 'pandas.core.frame.DataFrame'>
0x1e72b0426c8
# ilocは関数じゃない
d = df.iloc(0)
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.indexing._iLocIndexer'>
<pandas.core.indexing._iLocIndexer object at 0x000001E72B3F4458>
0x1e72b3f4458

列指定

# ilocによる取り出し
# 列指定(範囲)
d = df.iloc[:,2]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
0      3
1     30
2    300
Name: 2, dtype: int64
0x1e72a91e048
# ilocによる取り出し
# 列指定(範囲)
d = df.iloc[:,2:3]       #終端は含まないので注意
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     2
0    3
1   30
2  300
0x1e72a8a8a08
# ilocによる取り出し
# 列指定(範囲)
d = df.iloc[:, [True, False, True, False, True]]    # 列の数分のTrue/False指定が無いとエラーになる
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    2    4
0    1    3    5
1   10   30   50
2  100  300  500
0x1e72a959608
# ilocによる取り出し
# 列指定(範囲)
d = df.iloc[:, [0,2]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    2
0    1    3
1   10   30
2  100  300
0x1e72a941348
# ilocによる取り出し
# 列指定(範囲)
# [:,]が無いと行指定になる
d = df.iloc[[0,2]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
2  100  200  300  400  500
0x1e72b02e348
# ilocによる取り出し
# 列指定(範囲)
d = df.iloc[:, [2]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     2
0    3
1   30
2  300
0x1e72a931c08

行指定

# ilocによる取り出し
# 行指定
d = df.iloc[0]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
0    1
1    2
2    3
3    4
4    5
Name: 0, dtype: int64
0x1e72a909948
# ilocによる取り出し
# 行指定
d = df.iloc[[0]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
   0  1  2  3  4
0  1  2  3  4  5
0x1e72a921648
# ilocによる取り出し
# 行指定(個別指定)
d = df.iloc[[0,2]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
2  100  200  300  400  500
0x1e72a91dc88
# ilocによる取り出し
# 行指定(範囲指定)
d = df.iloc[0:2]     # ここは[[]] でない事に注意
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
    0   1   2   3   4
0   1   2   3   4   5
1  10  20  30  40  50
0x1e72a921208

# ilocによる取り出し

# 行指定(範囲指定)

d = df.iloc[[0:2]]     \# [[]]にするとエラーになる
print(type(d))
print(d)

File "", line 3
d = df.iloc[[0:2]] # [[]]にするとエラーになる
^
SyntaxError: invalid syntax

行と列の指定

# ilocによる取り出し
# 行指定(指定した行と列範囲)
d = df.iloc[0,2:4]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
2    3
3    4
Name: 0, dtype: int64
0x1e72a909888
# ilocによる取り出し
# 行指定(指定した行と列範囲)
d = df.iloc[0,[2,4]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
2    3
4    5
Name: 0, dtype: int64
0x1e72a941ac8
# ilocによる取り出し
# 行指定(指定した列と行範囲)
d = df.iloc[0:2,2]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
0     3
1    30
Name: 2, dtype: int64
0x1e72a97f148
# ilocによる取り出し
# 行指定(指定した列と行範囲)
d = df.iloc[[0,2],2]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
0      3
2    300
Name: 2, dtype: int64
0x1e72a8a8348
# ilocによる取り出し
# 行指定(指定した行範囲と列範囲)
d = df.iloc[0:2,2:4] 
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
    2   3
0   3   4
1  30  40
0x1e72a95b848
# ilocによる取り出し
# 行指定(指定した行と列)
d = df.iloc[0,2] 
print(type(d))
print(d)
print(hex(id(d)))
<class 'numpy.int64'>
3
0x1e72ad9af50
# ilocによる取り出し
# 行指定(指定した行と列)
d = df.iloc[[0],2] 
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
0    3
Name: 2, dtype: int64
0x1e72a942c48
# ilocによる取り出し
# 行指定(指定した行と列)
d = df.iloc[0,[2]]
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.series.Series'>
2    3
Name: 0, dtype: int64
0x1e72a930b48
# ilocによる取り出し
# 行指定(指定した行と列)
d = df.iloc[[0],[2]] 
print(type(d))
print(d)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
   2
0  3
0x1e72b037c48

代入

列指定

# ilocによる代入
# 列指定(範囲)
df1 = df.copy()

df1.iloc[:,2] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20  1000   40   50
2  100  200  1000  400  500
0x1e72b037c48
# ilocによる代入
# 列指定(範囲)
df1 = df.copy()
df1.iloc[:,2:3] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20  1000   40   50
2  100  200  1000  400  500
0x1e72b037c48
# ilocによる代入
# 列指定(範囲)
df1 = df.copy()
df1.iloc[:, [True, False, True, False, True]]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
      0    1     2    3     4
0  1000    2  1000    4  1000
1  1000   20  1000   40  1000
2  1000  200  1000  400  1000
0x1e72b037c48
# ilocによる代入
# 列指定(範囲)
df1 = df.copy()
df1.iloc[:, [0,2]]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
      0    1     2    3    4
0  1000    2  1000    4    5
1  1000   20  1000   40   50
2  1000  200  1000  400  500
0x1e72b037c48
# ilocによる代入
# 列指定(範囲)
df1 = df.copy()
df1.iloc[:, [2]]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20  1000   40   50
2  100  200  1000  400  500
0x1e72b037c48

行指定

# ilocによる代入
# 行指定
df1 = df.copy()
df1.iloc[0]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
      0     1     2     3     4
0  1000  1000  1000  1000  1000
1    10    20    30    40    50
2   100   200   300   400   500
0x1e72b037c48
# ilocによる代入
# 行指定
df1 = df.copy()
df1.iloc[[0]]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
      0     1     2     3     4
0  1000  1000  1000  1000  1000
1    10    20    30    40    50
2   100   200   300   400   500
0x1e72b037c48
# ilocによる代入
# 行指定(個別指定)
df1 = df.copy()
df1.iloc[[0,2]]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
      0     1     2     3     4
0  1000  1000  1000  1000  1000
1    10    20    30    40    50
2  1000  1000  1000  1000  1000
0x1e72b037c48
# ilocによる代入
# 行指定(範囲指定)
df1 = df.copy()
df1.iloc[0:2] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
      0     1     2     3     4
0  1000  1000  1000  1000  1000
1  1000  1000  1000  1000  1000
2   100   200   300   400   500
0x1e72b037c48

行と列の指定

# ilocによる代入
# 行指定(指定した行と列範囲)
df1 = df.copy()
df1.iloc[0,2:4] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2     3    4
0    1    2  1000  1000    5
1   10   20    30    40   50
2  100  200   300   400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行と列範囲)
df1 = df.copy()
df1.iloc[0,[2,4]] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3     4
0    1    2  1000    4  1000
1   10   20    30   40    50
2  100  200   300  400   500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行と列範囲)
df1 = df.copy()
df1.iloc[0:2,2] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20  1000   40   50
2  100  200   300  400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した列と行範囲)
df1 = df.copy()
df1.iloc[[0,2],2] = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200  1000  400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行範囲と列範囲)
df1 = df.copy()
df1.iloc[0:2,2:4]  = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2     3    4
0    1    2  1000  1000    5
1   10   20  1000  1000   50
2  100  200   300   400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行と列)
df1 = df.copy()
df1.iloc[0,2]   = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行と列)
df1 = df.copy()
df1.iloc[[0],2]    = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行と列)
df1 = df.copy()
df1.iloc[0,[2]]    = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72b037c48
# ilocによる代入
# 行指定(指定した行と列)
df1 = df.copy()
df1.iloc[[0],[2]]     = 1000
print(type(df1))
print(df1)
print(hex(id(d)))
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72b037c48

関数への引数

def for_Int(d):
    print("----for_Int--")
    print(type(d))
    print(hex(id(d)))
    d = 1000
    print(d)
    print("----for_Int--")
    
def for_Series(d):
    print("----for_Series--")
    print(type(d))
    print(hex(id(d)))
    d[0] = 1000
    print(d)
    print("----for_Series--")

    
def for_DataFrameIat(d):
    print("----for_DataFrameIat--")
    print(type(d))
    print(hex(id(d)))
    d.iat[0,0] = 1000
    print(d)
    print("----for_DataFrameIat--")
    
def for_DataFrameIloc(d):
    print("----for_DataFrameIloc--")
    print(type(d))
    print(hex(id(d)))
    d.iloc[0,0] = 1000
    print(d)
    print("----for_DataFrameIloc--")

列指定

# 列指定
df1 = df.copy()

for_Series(df1.iloc[:,2])
print(type(df1))
print(df1)
print(hex(id(d)))
----for_Series--
<class 'pandas.core.series.Series'>
0    1000
1      30
2     300
Name: 2, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72b037c48
# 列指定(範囲)
df1 = df.copy()
for_DataFrameIat(df1.iloc[:,2:3])
print(type(df1))
print(df1)
print(hex(id(df1)))

df2 = df.copy()
for_DataFrameIloc(df2.iloc[:,2:3])
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
      2
0  1000
1    30
2   300
----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72a95b288
----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
      2
0  1000
1    30
2   300
----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72a95b288


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:19: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

関数内においてilocで代入すると警告が表示される

# 列指定(範囲)
df1 = df.copy()
for_DataFrameIat(df1.iloc[:, [True, False, True, False, True]])
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[:, [True, False, True, False, True]])
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
      0    2    4
0  1000    3    5
1    10   30   50
2   100  300  500
----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a909188
----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
      0    2    4
0  1000    3    5
1    10   30   50
2   100  300  500
----for_DataFrame--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a91d948


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:19: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
# 列指定(範囲)
df1 = df.copy()
for_DataFrameIat(df1.iloc[:, [0,2]])
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[:, [0,2]])
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
0x1e72b216248
      0    2
0  1000    3
1    10   30
2   100  300
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a911848
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
0x1e72b216348
      0    2
0  1000    3
1    10   30
2   100  300
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72b216848


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:22: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
# 列指定(範囲)
df1 = df.copy()
for_DataFrameIat(df1.iloc[:, [2]])
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[:, [2]])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
      2
0  1000
1    30
2   300
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72b02ef48
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
      2
0  1000
1    30
2   300
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a8a6448


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:19: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

行指定

df1 = df.copy()
for_Series(df1.iloc[0])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_Series--
<class 'pandas.core.series.Series'>
0    1000
1       2
2       3
3       4
4       5
Name: 0, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
      0    1    2    3    4
0  1000    2    3    4    5
1    10   20   30   40   50
2   100  200  300  400  500
0x1e72b042608
df1 = df.copy()
for_DataFrameIat(df1.iloc[[0]] )
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[[0]] )
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
0x1e72a91e4c8
      0  1  2  3  4
0  1000  2  3  4  5
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72b042b08
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
0x1e72a909c88
      0  1  2  3  4
0  1000  2  3  4  5
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a95b0c8


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:22: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
# 行指定(個別指定)
df1 = df.copy()
for_DataFrameIat(df1.iloc[[0,2]] )
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[[0,2]] )
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
0x1e72b042f48
      0    1    2    3    4
0  1000    2    3    4    5
2   100  200  300  400  500
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a959a08
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
0x1e72a941788
      0    1    2    3    4
0  1000    2    3    4    5
2   100  200  300  400  500
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72b0421c8


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:22: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
# 列指定(範囲)
df1 = df.copy()
for_DataFrameIat(df1.iloc[0:2])
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[0:2])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
0x1e72a942c48
      0   1   2   3   4
0  1000   2   3   4   5
1    10  20  30  40  50
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
      0    1    2    3    4
0  1000    2    3    4    5
1    10   20   30   40   50
2   100  200  300  400  500
0x1e72a942d08
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
0x1e72a93cd48
      0   1   2   3   4
0  1000   2   3   4   5
1    10  20  30  40  50
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
      0    1    2    3    4
0  1000    2    3    4    5
1    10   20   30   40   50
2   100  200  300  400  500
0x1e72a8b1fc8


C:\Users\akira\Anaconda3\lib\site-packages\pandas\core\indexing.py:671: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  self._setitem_with_indexer(indexer, value)
C:\Users\akira\Anaconda3\lib\site-packages\ipykernel_launcher.py:22: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

行と列の指定

# 行指定(指定した行と列範囲)
df1 = df.copy()
for_Series(df1.iloc[0,2:4])
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_Series--
<class 'pandas.core.series.Series'>
0x1e72b216348
2       3
3       4
0    1000
Name: 0, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72b2169c8
# 行指定(指定した行と列範囲)
df1 = df.copy()
for_Series(df1.iloc[0,[2,4]])
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_Series--
<class 'pandas.core.series.Series'>
0x1e72b216208
2       3
4       5
0    1000
Name: 0, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a92edc8
# 行指定(指定した行と列範囲)
df1 = df.copy()
for_Series(df1.iloc[0:2,2])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_Series--
<class 'pandas.core.series.Series'>
0x1e72a955708
0    1000
1      30
Name: 2, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72b2164c8
# 行指定(指定した列と行範囲)
df1 = df.copy()
for_Series(df1.iloc[[0,2],2])
print(type(df1))
print(df1)
print(hex(id(df1)))

----for_Series--
<class 'pandas.core.series.Series'>
0x1e72adfe188
0    1000
2     300
Name: 2, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a930d08
# 行指定(指定した行範囲と列範囲)
df1 = df.copy()
for_DataFrameIat(df1.iloc[0:2,2:4])
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[0:2,2:4])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
0x1e72a929c08
      2   3
0  1000   4
1    30  40
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72a929388
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
0x1e72a91b1c8
      2   3
0  1000   4
1    30  40
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
     0    1     2    3    4
0    1    2  1000    4    5
1   10   20    30   40   50
2  100  200   300  400  500
0x1e72a8e6e48
# 行指定(指定した行と列)
df1 = df.copy()
for_Int(df1.iloc[0,2])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_Int--
<class 'numpy.int64'>
0x1e72b3f0d10
1000
----for_Int--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a8bedc8
# 行指定(指定した行と列)
df1 = df.copy()
for_Series(df1.iloc[[0],2])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_Series--
<class 'pandas.core.series.Series'>
0x1e72b08c108
0    1000
Name: 2, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a8c4188
# 行指定(指定した行と列)
df1 = df.copy()
for_Series(df1.iloc[0,[2]] )
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_Series--
<class 'pandas.core.series.Series'>
0x1e72a91d648
2       3
0    1000
Name: 0, dtype: int64
----for_Series--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a97f5c8
# 行指定(指定した行と列)
df1 = df.copy()
for_DataFrameIat(df1.iloc[[0],[2]])
print(type(df1))
print(df1)
print(hex(id(df1)))

df1 = df.copy()
for_DataFrameIloc(df1.iloc[[0],[2]])
print(type(df1))
print(df1)
print(hex(id(df1)))
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
0x1e72a921248
      2
0  1000
----for_DataFrameIat--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a942d08
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
0x1e72a8ed708
      2
0  1000
----for_DataFrameIloc--
<class 'pandas.core.frame.DataFrame'>
     0    1    2    3    4
0    1    2    3    4    5
1   10   20   30   40   50
2  100  200  300  400  500
0x1e72a9410c8
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?