[Python] Summary of table creation method using DataFrame (pandas)

[Python] Summary of table creation method using DataFrame (pandas)

There are multiple ways to create a single table using Pandas DataFrame.

Summary of basic method of making.

The fastest way to read the base table is to create it as a csv file.


**table of contents**
  1. [Table to be created](# 1 Table to be created)
  2. [Create from 2D list](# 22 Create from 2D list)
  3. [Specify matrix name later](# 1 Matrix name specified later)
  4. [Specify as an option when creating a table](# 2 Specify as an option when creating a table)
  5. [Specify the column that will be the row name from the table](# 3 Specify the column that will be the row name from the table)
  6. [Create from 1D list](Create from # 31D list)
  7. [Combine each list together](# 1 Combine each list together)
  8. [Combine each list one by one](# 2 Join each list one by one)
  9. [Merge with formula](# 3 Combine with formula)
  10. [Create and read with csv file](# 4 Create and read with csv file)
  11. [List of methods used](# 5 List of methods used)

## 1. 1. Table to create Create the table below.
image.png

--Column name: row0 ~ row5 --Line name: col0 ~ col4


## 2. Created from a 2D list ** ■ What is a two-dimensional array? ** `[['A','B',,],['C','D',,],,,]`

A one-dimensional list ['A','B' ,,] in the same [] in parallel.

Check with the following 3 patterns. (1) Specify the matrix name later (2) Optional when creating a table (3) Specify the column that will be the row name from the table


### (1) Specify the matrix name later

** ▼ Procedure ** ① Make each line one list ② Convert to table ③ Change the line name ④ Change column name


** ① Arrange each line in parallel as one list **

Two-dimensional array


list = [
[1, 100, 0.33, 'AAA', 'AAA100'], 
[2, 200, 0.67, 'BBB', 'BBB200'], 
[3, 300, 1, 'CCC', 'CCC300'], 
[4, 400, 1.33, 'DDD', 'DDD400'], 
[5, 500, 1.67, 'EEE', 'EEE500'], 
[6, 600, 2, 'FFF', 'FFF600']]

** ② Convert to table ** `pd.DataFrame(list)` └ Convert to a table with DataFrame of pandas.

Convert to table


df = pd.DataFrame(list)
df
image.png

Row and column names will be automatically assigned numbers.


** ③ Change line name ** `df.index = ['A','B',,,]`

-Change the line name (index) to the specified name. -Match the number of ** elements in [] with the number of rows **. Otherwise an error.

Rename line


df.index = ['row0','row1','row2','row3','row4','row5']
df
image.png

** ④ Change column name ** `df.columns = ['a','b',,,]`

-Change the column name to the specified name. -Match the number of ** elements in [] with the number of rows **. Otherwise an error.

Rename column


df.columns = ['col0','col1','col2','col3','col4']
df
image.png

This completes.


### (2) Optional when creating a table

** ▼ Procedure ** ① Make each line one list (2) Convert to table (specify matrix name as an option) 2-1 columns option 2-2 index option


** ① Arrange each line in parallel as one list **

Two-dimensional array


list = [
[1, 100, 0.33, 'AAA', 'AAA100'], 
[2, 200, 0.67, 'BBB', 'BBB200'], 
[3, 300, 1, 'CCC', 'CCC300'], 
[4, 400, 1.33, 'DDD', 'DDD400'], 
[5, 500, 1.67, 'EEE', 'EEE500'], 
[6, 600, 2, 'FFF', 'FFF600']]

** ② Convert to table (specify matrix name as an option) ** `pd.DataFrame(list, index=['A','B',,,], columns=['a','b',,,])` └ "list": Array to be tabulated └ index = ['A','B' ,,,]: index option └ columns = ['a','b' ,,,]: columns option └ Variables can be specified in the option [].

Optional matrix name


ind = ['row0','row1','row2','row3','row4','row5']
col = ['col0','col1','col2','col3','col4']

df = pd.DataFrame(list, index=ind, columns=col)
df
image.png

This completes.


### (3) Specify the column that will be the row name from the table

** ▼ Procedure ** ① Make each line one list ② Convert to table ③ Change the column name ④ Specify the column that will be the row name


** ① Make each line into one list ** Include row names in a two-dimensional array.

Two-dimensional array


list = [
['row0', 1, 100, 0.33, 'AAA', 'AAA100'], 
['row1', 2, 200, 0.67, 'BBB', 'BBB200'], 
['row2', 3, 300, 1, 'CCC', 'CCC300'], 
['row3', 4, 400, 1.33, 'DDD', 'DDD400'], 
['row4', 5, 500, 1.67, 'EEE', 'EEE500'], 
['row5', 6, 600, 2, 'FFF', 'FFF600'], ]

** ② Convert to table ** `pd.DataFrame(list)` └ "list": Variable containing array data

Convert to table


df = pd.DataFrame(list)
df
image.png

Row and column names will be automatically assigned numbers.


** ③ Change column name ** `df.columns = ['a','b',,,]`

-Change the column name to the specified name. -Match the number of ** elements in [] with the number of rows **. Otherwise an error.

Rename column


df.columns =  ['','col0','col1','col2','col3','col4']
df
image.png

** ④ Specify the column that will be the row name ** `set_index ('column name')`

Specify the column that will be the row name


df = df.set_index('')
df
image.png

The column name "''" was set in the heading.


## 3.1 Created from a one-dimensional list

Check with the following 3 patterns. (1) Combine each list together (2) Combine each list one by one (3) Combine with mathematical formula


### (1) Combine each list together

** ▼ Procedure ** ① Create a list for each line ② Convert to a table at once ③ Change the line name ④ Change column name


** ① Create a list for each line **

create list


listA = [1, 100, 0.33, 'AAA', 'AAA100']
listB = [2, 200, 0.67, 'BBB', 'BBB200']
listC = [3, 300, 1, 'CCC', 'CCC300']
listD = [4, 400, 1.33, 'DDD', 'DDD400']
listE = [5, 500, 1.67, 'EEE', 'EEE500']
listF = [6, 600, 2, 'FFF', 'FFF600']

** ② Convert all together to a table ** `pd.DataFrame([A,B,,,])` └ "A" "B" one-dimensional list └ The number of elements is equal

Convert to a table at once


pd.DataFrame([listA,listB,listC,listD,listE,listF])
image.png

** ③ Change line name ** `df.index = ['A','B',,,]`

-Change the line name (index) to the specified name. -Match the number of ** elements in [] with the number of rows **. Otherwise an error.

Rename line


df.index = ['row0','row1','row2','row3','row4','row5']
df
image.png

** ④ Change column name ** `df.columns = ['a','b',,,]`

-Change the column names to the specified name. -Match the number of ** elements in [] with the number of rows **. Otherwise an error.

Rename column


df.columns = ['col0','col1','col2','col3','col4']
df
image.png

This completes.


### (2) Combine each list one by one

** ▼ Procedure ** ① Create a list for each line ② Create a table ③ Add a column to the table ④ Change the line name ⑤ Transpose


** ① Create a list for each line **

create list


listA = [1, 100, 0.33, 'AAA', 'AAA100']
listB = [2, 200, 0.67, 'BBB', 'BBB200']
listC = [3, 300, 1, 'CCC', 'CCC300']
listD = [4, 400, 1.33, 'DDD', 'DDD400']
listE = [5, 500, 1.67, 'EEE', 'EEE500']
listF = [6, 600, 2, 'FFF', 'FFF600']

** ② Creating a table ** Generate a table from one list. `pd.DataFrame(list, columns=['A'])` └ "listA": Array data └ "columns = ['A']": Set the column name to A

Creating a table


df= pd.DataFrame(listA, columns=['row0'])
df

image.png

└ Describe 'column name': [array] in ({})

Create with column name ①


pd.DataFrame({'row0':[1, 100, 0.33, 'AAA', 'AAA100']})
image.png

Create with column name (multiple columns)


pd.DataFrame({'row0':[1, 100, 0.33, 'AAA', 'AAA100'], 'row1':[2, 200, 0.67, 'BBB', 'BBB200']})
image.png

** ③ Add column to table ** `df['B'] = pd.DataFrame(list)` └ "B": Column name to add └ "list": Table data to be added

Add column to table


df['row1'] = pd.DataFrame(listB)
df['row2'] = pd.DataFrame(listC)
df['row3'] = pd.DataFrame(listD)
df['row4'] = pd.DataFrame(listE)
df['row5'] = pd.DataFrame(listF)
df

image.png


** ④ Change line name ** `df.index = ['A','B',,,]`

Rename line


df.index = ['col0','col1','col2','col3','col4']
df

image.png


** ⑤ Transpose ** `df.T` └ Swap the matrix.

Transpose


df = df.T
df
image.png

This completes.


### (3) Combine with mathematical formula If each column has regularity, you can use mathematical formulas to join the columns.
image.png

** ▼ Regularity **

** ▼ Procedure ** ① Create list ② Create a table ③ Add columns to the table using formulas ④ Change the line name


** ① Create list ** Create a base column.

Two for col0 and col3.

create list


listA = [1,2,3,4,5,6]
listD = ['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']

** ② Creating a table ** `pd.DataFrame(list, columns=['A'])` └ "listA": Array data └ "columns = ['A']": Set the column name to A

Creating a table


df = pd.DataFrame(listA, columns=['col0'])
df
image.png

** ③ Add columns to the table using formulas ** ・ `Df ['B'] = df ['A'] / 2` └ "B": Column name to add └ "df ['A']" / 2: Substitute the value of the existing column name A divided by 2.

Df ['C'] = df ['A'] + df ['B'] └ Combine the values in columns "A" and "B" └ * str + int is an error (match the type)

Df ['A']. Astype (str) └ Change column "A" to "str" type

Add columns to the table using formulas


df['col1'] = df['col0'] * 100
df['col2'] = df['col0'] / 3
df['col3'] = pd.DataFrame(listD)
df['col4'] = df['col3'] + df['col1'].astype(str)
df
image.png

** ④ Change line name ** `df.index = ['A','B',,,]`

Rename line


df.index=['row0','row1','row2','row3','row4','row5']
df
image.png

This completes.


## 4. Create and read as a csv file

** ▼ Procedure ** ① Create a table with a csv file ② Import with read_csv method └ Specify heading column


** ① Create a table with a csv file ** Create a table like the one below.

image.png


** ② Import with read_csv method ** `read_csv ('falpath', index_col = n)` └ "'File path'": Absolute path or relative path is OK └ "index_col = n": Specify the column number (n: integer) to be the heading
** ▼ When reading the test.csv file on the desktop **

Read csv file


import pandas as pd
pd.read_csv('~/desktop/test.csv', index_col=0)
image.png

This completes.

Click here for a list of options for reading csv files (https://qiita.com/yuta-38/items/e1e890a647e77c7ccaad)


## 5. List of methods used ・ `Pd.DataFrame (list)` └ Convert list to table

Pd.DataFrame ([A, B ,,,]) └ Convert one-dimensional list to table at once

Df.index = ['A','B' ,,,] └ Change line name

Df.columns = ['a','b' ,,,] └ Change column name

Pd.DataFrame (list, index = ['A','B' ,,,]) └ Specify the row name when creating the table

Pd.DataFrame (list, columns = ['a','b' ,,,]) └ Specify the column name when creating the table

pd.DataFrame ({'A': [1,2 ,,],'B': [3,4 ,,],}) └ Specify the column name when creating the table └ Describe 'column name': [array] in ({})

Set_index ('a') └ Specify the column that will be the row name

Df ['b'] = pd.DataFrame (list) └ Add column (column name b) to table (df)

Df.T └ Transpose

Df ['B'] = df ['A'] / 2 └ "B": Column name to add └ "df ['A']" / 2: Substitute the value of the existing column name A divided by 2.

Df ['C'] = df ['A'] + df ['B'] └ Combine the values in columns "A" and "B" └ * str + int is an error (match the type)

Df ['A']. Astype (str) └ Change column "A" to "str" type

[['A','B' ,,], ['C','D' ,,] ,,,] └ Two-dimensional array


[Return to top](#)

Recommended Posts

[Python] Summary of table creation method using DataFrame (pandas)
[Python] Operation memo of pandas DataFrame
[Pandas_flavor] Add a method of Pandas DataFrame
Python pandas: Search for DataFrame using regular expressions
[python] Create table from pandas DataFrame to postgres
[Python] Sort the table by sort_values (pandas DataFrame)
Summary of Excel operations using OpenPyXL in Python
Basic operation of Python Pandas Series and Dataframe (1)
Summary of Python arguments
Python application: Pandas # 3: Dataframe
Summary of test method
Comparison of Python (+ Pandas), R, Julia (+ DataFrames) (summary of table contents, access by column)
Summary of Python sort (list, dictionary type, Series, DataFrame)
Summary of Pandas methods used when extracting data [Python]
[Python] Using Line API [1st Creation of Beauty Bot]
Summary of things that were convenient when using pandas
Summary of Python3 list operations
Formatted display of pandas DataFrame
python: Basics of using scikit-learn ①
python pandas study recent summary
Basic usage of Pandas Summary
Behavior of pandas rolling () method
Data analysis using python pandas
The Power of Pandas: Python
Convert from Pandas DataFrame to System.Data.DataTable using Python for .NET
[Python] Random data extraction / combination from DataFrame using random and pandas
Basic summary of data manipulation with Python Pandas-First half: Data creation & manipulation
Python hand play (Pandas / DataFrame beginning)
[Python] Loading csv files using pandas
Image capture of firefox using python
Summary if using AWS Lambda (Python)
Data visualization method using matplotlib (+ pandas) (5)
[Python] [Table of Contents Links] Python Programming
A brief summary of Python collections
Removal of haze using Python detailEnhanceFilter
[Python] How to add rows and columns to a table (pandas DataFrame)
Python Math Series ⓪ Table of Contents
Implementation of desktop notifications using Python
Excel graph creation using python xlwings
Summary of differences between Python and PHP (comparison table of main items)
Introductory table of contents for python3
[Python] Multiplication table using for statement
[python] -1 meaning of numpy's reshape method
Data visualization method using matplotlib (+ pandas) (4)
Summary of Python indexes and slices
[OpenCV; Python] Summary of findcontours function
GUI creation in python using tkinter 2
Installation method using the pip command of the Python package (library) Mac environment
[Python] How to read a csv file (read_csv method of pandas module)
Get images of great find / 47 sites using Python (1/2: until target list creation)
Python: Basics of image recognition using CNN
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
[Python] What is pandas Series and DataFrame?
About building GUI using TKinter of Python
GUI creation in python using tkinter part 1
Summary of methods often used in pandas
(Bad) practice of using this in Python
Summary of gamma distribution parameter specification method
Python + Selenium Frequently used operation method summary
Flexible animation creation using animation.FuncAnimation of matplotlib
Python: Application of image recognition using CNN