[Python] How to get & change rows / columns / values from a table.

[Python] How to get & change rows / columns / values from a table

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 of contents**
  1. [Extract column](# 1 extract column)
  2. [Extract column name list](# Extract column name list)
  3. [Extract by column name](# Extract by column name)
  4. [Extract by column number](# Extract by column number)
  5. [Extract Rows](# 2 Extract Rows)
  6. [Extract line name list](# Extract line name list)
  7. [Extract by line name](# Extract by line name)
  8. [Extract by line number](# Extract by line number)
  9. [Extract Element](# 3 Extract Element)
    1. loc
    2. iloc
    3. at
    4. iat
  10. [Change data (change by specifying the element)](# 4 Change data)
    1. loc
    2. iloc
    3. at
    4. iat

## Table to use Extract by specifying the elements from the table created with 4 rows and 4 columns.

▼ Table to be used (assigned to variable "df") image.png

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


## 1. 1. Column extraction ① Extraction of column name list ② Extract by column name ③ Extract by column number

▼ ① Extract the column name list

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']

** ▼ Output only the heading of the specified column number ** `df.columns[n]` └ "n": Sequence number

Extract by column number


df.columns[2]

#output
# 'ccc'

### ② Extract by column name There are three ways to write by column name ②-1. df ['column name'] ②-2. Df. Column name ②-3. df.loc [:, ['column name']]

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

** ▼ Extract with loc method **

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


### ③ Extract by column number `iloc[:,[n,x,,,]]` └ "iloc []": Specify by column number └ ":,": No line specified └ "n" "x": column number └ Slice [n: x] cannot be used (error)

Extract with iloc


df.iloc[:, [1,2]]


#output

	bbb	ccc
111	EEE	III
222	FFF	JJJ
333	GGG	KKK
444	HHH	LLL

** ▼ 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

## 2. Extraction of rows ① Extraction of line name list ② Extract by line name ③ Extract by line number

▼ ① Extraction of line name list

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]

** ▼ Output only the heading of the specified line number ** `df.index[n]` └ "n": Line number

Extract by line number


df.index[2]

#output
# '333'

### ② Extract by line name `df.loc[['aaa', 'bbb',,,,]]` └ "df": Variable with table └ ".loc []": How to specify row / column names └ "aaa" "bbb": line name

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 [line name:]`

Extract after the specified line


df.loc[333:]


#output

	aaa	bbb	ccc	ddd
333	CCC	GGG	KKK	OOO
444	DDD	HHH	LLL	PPP

** ▼ In case of only one line ** -One line is described in two patterns. -The output format changes (column name, dtype, etc.) `df.loc[[111]]` `df.loc[111]` └ Double parentheses are easier to see.
** ▼ Example of `df.loc [[111]]` **

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

③ Extract by line number

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

About slices


** ▼ Example: df [1: 3] ** Extract the 1st and 2nd lines. The 3rd line is not included (because the processing ends at the 3rd line)

Extraction of rows


df[1:3]

#output

	aaa	bbb	ccc	ddd
222	BBB	FFF	JJJ	NNN
333	CCC	GGG	KKK	OOO

** ▼ Example: Specify the amount of change and extract **

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

** ▼ When the initial value is omitted ** The initial value is regarded as 0, and the lines up to the specified number (one before) from the top can be extracted.

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

### (3-2) Extract with iloc

df.iloc[[n,m,,,]] └ "df": Variable with table └ ".iloc []": Method specified by row / column number └ "n" "m": Specify line number


▼ Example: `df.iloc [[1,3]]`

Specified by line number


df.iloc[[1,3]]

#output
	aaa	bbb	ccc	ddd
222	BBB	FFF	JJJ	NNN
444	DDD	HHH	LLL	PPP

#### When extracting only one line There are two patterns of description. `df.iloc[[n]]` `df.iloc[n]`

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

** ▼ Example: `df.iloc [n]` ** Output for single parentheses.

Specified by line number


df.iloc[3]

#output
aaa    DDD
bbb    HHH
ccc    LLL
ddd    PPP
Name: 444, dtype: object

## 3. 3. Extraction of elements There are four main methods to use. ①loc ②iloc ③at ④iat

** ■ 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


### ▼ Extraction source table ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/563526/cb3b210a-31af-2069-e91d-14e01ea77047.png)

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'

(3-2) iloc `df.iloc ['row number','column number']`

iloc


df.iloc[2,1]

#output
# 'GGG'

(3-3) at `df.at ['row name','column name']`

at


df.at[333,'bbb']

#output
# 'GGG'

(3-4) iat `df.at ['row number','column number']`

iat


df.iat[2,1]

#output
# 'GGG'

## 4. Data change ### Change by specifying an element If you specify an element and substitute it, the data will be replaced. There are four main methods to use.

①loc ②iloc ③at ④iat

Details
** ■ 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.

** ■ 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


### Original table Use the table below to rewrite the data. ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/563526/cb3b210a-31af-2069-e91d-14e01ea77047.png)

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

### ■ Change row name and column name ① When creating a table Use DataFrame options · Columns: columns option · Row: index option

② 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 **



> Official page -[Pandas.DataFrame.loc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html#pandas.DataFrame.loc) -[Pandas.DataFrame.iloc](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html#pandas.DataFrame.iloc) -[Pandas.DataFrame.at](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.at.html#pandas.DataFrame.at) -[Pandas.DataFrame.iat](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iat.html#pandas.DataFrame.iat)
[Back to top](# How to get and change matrix values from python table)

Recommended Posts

[Python] How to get & change rows / columns / values from a table.
[Python] How to add rows and columns to a table (pandas DataFrame)
How to get a stacktrace in python
How to get a string from a command line argument in python
[Python] How to delete rows and columns in a table (list of drop method options)
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
How to open a web browser from python
How to generate a Python object from JSON
How to get a value from a parameter store in lambda (using python)
How to save a table scraped by python to csv
How to change Python version
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
How to slice a block multiple array from a multiple array in Python
How to run a Python program from within a shell script
I tried "How to get a method decorated in Python"
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
[Python] How to output a pandas table to an excel file
How to get a list of links from a page from wikipedia
How to write a Python class
How to get started with Python
[Python] How to swap array values
How to access wikipedia from python
Did not change from Python 2 to 3
How to get all the possible values in a regular expression
How to use Visual Recognition to get LINE ID from a girl
[Python] How to get a value with a key other than value with Enum
How to get a job as an engineer from your 30s
How to remove duplicates from a Python list while preserving order.
How to create a clone from Github
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
Send a message from Python to Slack
[Python] How to invert a character string
How to display multiplication table in python
How to access RDS from Lambda (python)
How to create a repository from media
How to run a Maya Python script
[Python] How to extract / delete / convert a matrix containing missing values (NaN)
A story I was addicted to when inserting from Python to a PostgreSQL table
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
How to get a namespaced view name from a URL (path_info) in Django
How to get a sample report from a hash value using VirusTotal's API
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to clear tuples in a list (Python)
How to embed a variable in a python string
How to create a function object from a string
[Python] How to change the date format (display format)
Study from Python Hour7: How to use classes
How to get results from id in Celery
How to create a JSON file in Python
[Python] How to read data from CIFAR-10 and CIFAR-100
[python] Create table from pandas DataFrame to postgres
[Bash] Use here-documents to get python power from bash
How to add a Python module search path
How to handle Linux commands well from Python
How to extract coefficients from a fractional formula