From a table created with Pandas DataFrame ・ ** How to extract the specified element ** -** Summary of how to change the value of the specified element **.
▼ Table to be used (assigned to variable "df")
Table creation code
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
listC = ['III', 'JJJ', 'KKK', 'LLL']
listD = ['MMM', 'NNN', 'OOO', 'PPP']
df = pd.DataFrame(listA)
df[1] = listB
df[2] = listC
df[3] = listD
df.columns = ['aaa', 'bbb', 'ccc', 'ddd']
df.index = [111,222,333,444]
df
For details on how to create a table, click here ()
Use the .columns
method
└ Extraction of column name list
└ Output is Index / Int64Index type, etc.
└ Column type (dtype) is also output
[Table to use and its code](#Table to use)
Extract column names
df.columns
#output
# Index(['aaa', 'bbb', 'ccc', 'ddd'], dtype='object')
The output will be of type Index.
▼ Index type can be list.
Output as list type
list(df.columns)
#output
# ['aaa', 'bbb', 'ccc', 'ddd']
Extract by column number
df.columns[2]
#output
# 'ccc'
Extract by column name ①
df['bbb']
#output
111 EEE
222 FFF
333 GGG
444 HHH
Name: bbb, dtype: object
Extract by column name ②
df.bbb
#output
111 EEE
222 FFF
333 GGG
444 HHH
Name: bbb, dtype: object
Quotation marks are not required even for character strings. If you turn it on, an error will occur.
error
df.'bbb'
#output
# invalid syntax
df.loc[:, ['aaa', 'bbb',,,]]
└ "loc []": loc method
└ ":,": No line name specified
└ "['aaa','bbb' ,,,]": Specify the column name you want to extract.
Extract by column name ③ (loc)
df.loc[:, ['bbb', 'ccc']]
bbb ccc
111 EEE III
222 FFF JJJ
333 GGG KKK
444 HHH LLL
df.loc[:, 'aaa']
=df.loc[:, ['aaa']]
Extract with iloc
df.iloc[:, [1,2]]
#output
bbb ccc
111 EEE III
222 FFF JJJ
333 GGG KKK
444 HHH LLL
df.loc[:, 2]
= df.loc[:, [2]]
** ▼ Extract only one column **
Extract with iloc (only one column ①)
df.iloc[:, 2]
#output
111 III
222 JJJ
333 KKK
444 LLL
Name: ccc, dtype: object
Extract with iloc (only one column ②)
df.iloc[:, [2]]
#output
ccc
111 III
222 JJJ
333 KKK
444 LLL
Use the .index
method
└ Extraction of column name list
└ Output is Index / Int64Index type, etc.
└ Column type (dtype) is also output
[Table to use and its code](#Table to use)
Extract line name
df.index
#output
# Int64Index([111, 222, 333, 444], dtype='int64')
In this case, the output will be Int64Index type.
▼ index / int64 Index type can be list.
Output as list type
list(df.index)
#output
# [111, 222, 333, 444]
Extract by line number
df.index[2]
#output
# '333'
Extract by line name
df.loc[[111,333,444]]
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
333 CCC GGG KKK OOO
444 DDD HHH LLL PPP
Extract after the specified line
df.loc[333:]
#output
aaa bbb ccc ddd
333 CCC GGG KKK OOO
444 DDD HHH LLL PPP
Extract with loc (only one column ①)
df.loc[[111]]
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
** ▼ Example of df.loc [111]
**
Extract with loc (only one column ②)
df.loc[111]
#output
aaa AAA
bbb EEE
ccc III
ddd MMM
Name: 111, dtype: object
③-1. df[n:m:l]
③-2. df.iloc[[n,m,,,,]]
(3-1)df[n:m:l]
Specify the rows to extract using slices.
└ "df": Variable with table
└ "n": Start value
└ "m": End price
└ "l": amount of change
・ "N" and "l" can be omitted.
Extraction of rows
df[1:3]
#output
aaa bbb ccc ddd
222 BBB FFF JJJ NNN
333 CCC GGG KKK OOO
df[1:5:2]
From the 1st to 4th lines, extract the lines with numbers incremented by 2.
Extraction of rows
df[1:5:2]
#output
aaa bbb ccc ddd
222 BBB FFF JJJ NNN
444 DDD HHH LLL PPP
df[:3]
Extract from 0 to 2nd line.
Extraction of rows
df[:3]
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
333 CCC GGG KKK OOO
df.iloc[[n,m,,,]]
└ "df": Variable with table
└ ".iloc []": Method specified by row / column number
└ "n" "m": Specify line number
Specified by line number
df.iloc[[1,3]]
#output
aaa bbb ccc ddd
222 BBB FFF JJJ NNN
444 DDD HHH LLL PPP
Difference in the number of []. The output result is different for each. └ The output is easier to see with df.iloc [[n]].
** ▼ Example: df.iloc [[n]]
**
Output for double parentheses.
Specified by line number
df.iloc[[3]]
#output
aaa bbb ccc ddd
444 DDD HHH LLL PPP
Specified by line number
df.iloc[3]
#output
aaa DDD
bbb HHH
ccc LLL
ddd PPP
Name: 444, dtype: object
** ■ Can be broadly classified into "loc" and "at" ** -"Loc": Specify by range. You can also get multiple rows and columns. -"At": Get only one element pinpoint.
〇Supplement: "at" is faster.
** ■ Presence or absence of "i" ** With or without the "i" at the beginning, you can choose to specify by name or by number.
・ None: Matrix name ・ Yes: Matrix number
Table creation code
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
listC = ['III', 'JJJ', 'KKK', 'LLL']
listD = ['MMM', 'NNN', 'OOO', 'PPP']
df = pd.DataFrame(listA)
df[1] = listB
df[2] = listC
df[3] = listD
df.columns = ['aaa', 'bbb', 'ccc', 'ddd']
df.index = [111,222,333,444]
df
(3-1) loc
df.loc ['row name','column name']
loc
df.loc[333,'bbb']
#output
# 'GGG'
iloc
df.iloc[2,1]
#output
# 'GGG'
at
df.at[333,'bbb']
#output
# 'GGG'
iat
df.iat[2,1]
#output
# 'GGG'
①loc ②iloc ③at ④iat
** ■ Presence or absence of "i" ** With or without the "i" at the beginning, you can choose to specify by name or by number.
・ None: Matrix name ・ Yes: Matrix number
Table creation code
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
listC = ['III', 'JJJ', 'KKK', 'LLL']
listD = ['MMM', 'NNN', 'OOO', 'PPP']
df = pd.DataFrame(listA)
df[1] = listB
df[2] = listC
df[3] = listD
df.columns = ['aaa', 'bbb', 'ccc', 'ddd']
df.index = [111,222,333,444]
df
(4-1) loc
loc
df.loc[333,'bbb'] = '@@@'
df
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
333 CCC @@@ KKK OOO
444 DDD HHH LLL PPP
(4-2) iloc
iloc
df.iloc[2,1] = '★★'
df
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
333 CCC ★★ KKK OOO
444 DDD HHH LLL PPP
(4-3) at
at
df.at[333,'bbb'] = 999
df
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
333 CCC 999 KKK OOO
444 DDD HHH LLL PPP
(4-4) iat
iat
df.iat[2,1] = '※※'
df
#output
aaa bbb ccc ddd
111 AAA EEE III MMM
222 BBB FFF JJJ NNN
333 CCC ※※ KKK OOO
444 DDD HHH LLL PPP
② Later (collectively) · Columns: columns method -Line: index method
③ Later (designated) -Rename method └ "columns = {}": Change column name └ "index = {}": Change line name
** ▶ How to change the row name and column name here **