0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pandasの要素指定・置換

Posted at

はじめに

Pandasで要素を指定したり、要素の値を置換する方法をまとめる。

目次

要素・範囲指定

種類 対象 指定方法 スライス , リスト
df.loc[] 範囲 , 複数 ラベル
df.iloc[] 範囲 , 複数 番号
df.at[] 要素 ラベル -
df.iat[] 要素 番号 -
import pandas as pd

df = pd.DataFrame({
	 'White':{'A':1,'B':2,'C':3,'D':4,'E':5}
	,'Black':{'A':10,'B':20,'C':30,'D':40,'E':50}
	,'Red'  :{'A':100,'B':200,'C':300,'D':400,'E':500}
	,'Green':{'A':1000,'B':2000,'C':3000,'D':4000,'E':5000}
	,'Blue' :{'A':10000,'B':20000,'C':30000,'D':40000,'E':50000}
	})


print(df)
#    White  Black  Red  Green   Blue
# A      1     10  100   1000  10000
# B      2     20  200   2000  20000
# C      3     30  300   3000  30000
# D      4     40  400   4000  40000
# E      5     50  500   5000  50000


#df.loc[]:行列名で範囲指定①-------------------
print(df.loc['A':'C','Red':'Blue'])

#    Red  Green   Blue
# A  100   1000  10000
# B  200   2000  20000
# C  300   3000  30000


#df.loc[]:行列名で範囲指定②-------------------
print(df.loc['B':,:'Green'])

#    White  Black  Red  Green
# B      2     20  200   2000
# C      3     30  300   3000
# D      4     40  400   4000
# E      5     50  500   5000


#df.loc[]:行列名で範囲指定③-------------------
print(df.loc[['B','C'],:'Green'])
#    White  Black  Red  Green
# B      2     20  200   2000
# C      3     30  300   3000


#df.iloc[]:行列番号で範囲指定①---------------
print(df.iloc[0:3,2:5])

#    Red  Green   Blue
# A  100   1000  10000
# B  200   2000  20000
# C  300   3000  30000


#df.iloc[]:行列番号で範囲指定②---------------
print(df.iloc[1:,:3])

#    White  Black  Red
# B      2     20  200
# C      3     30  300
# D      4     40  400
# E      5     50  500

#df.iloc[]:行列番号で範囲指定③---------------
print(df.iloc[1:4,[0,2,4]])

#    White  Red   Blue
# B      2  200  20000
# C      3  300  30000
# D      4  400  40000

#df.at[]:行列名で要素指定--------------------
print(df.at['D','White'])

# 4


#df.iloc[]:行列番号で要素指定----------------
print(df.iat[3,4])

# 40000

戻る

値の置換

replace()で変換ができる。引数にinplace=Trueを指定すれば、元のDataFrameが置換される。

import pandas as pd

df = pd.DataFrame({
	 'White':{'A':1,'B':2,'C':3}
	,'Black':{'A':1,'B':2,'C':3}
	,'Red'  :{'A':100,'B':200,'C':300}
	})

print(df)

#    White  Black  Red
# A      1      1  100
# B      2      2  200
# C      3      3  300


# 全ての要素:一つの値だけ変更
print(df.replace(1,11))

#    White  Black  Red
# A     11     11  100
# B      2      2  200
# C      3      3  300


# 全ての要素:複数の値を同時に変更
print(df.replace({1:11,2:22}))

#    White  Black  Red
# A     11     11  100
# B     22     22  200
# C      3      3  300


# 全ての要素:複数の値を同じ値に変更
print(df.replace([1,2,3],99))

#    White  Black  Red
# A     99     99  100
# B     99     99  200
# C     99     99  300


# 列を指定:一つの値だけ変更
print(df.replace({'Black': {1:11}}) )

#    White  Black  Red
# A      1     11  100
# B      2      2  200
# C      3      3  300


# 列を指定:複数の値を同時に変更
print(df.replace({'Black': {1:11, 2:22}}) )

#    White  Black  Red
# A      1     11  100
# B      2     22  200
# C      3      3  300


# 複数を指定:複数の値を同時に変更
print(df.replace({'Black': {1:11, 2:22}, 'Red': {100:111, 200:222}} ) )

#    White  Black  Red
# A      1     11  111
# B      2     22  222
# C      3      3  300

戻る

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?