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.
--Column name: row0 ~ row5 --Line name: col0 ~ col4
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
** ▼ Procedure ** ① Make each line one list ② Convert to table ③ Change the line name ④ Change column name
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
df = pd.DataFrame(list)
df
Row and column names will be automatically assigned numbers.
-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
-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
This completes.
** ▼ Procedure ** ① Make each line one list (2) Convert to table (specify matrix name as an option) 2-1 columns option 2-2 index option
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']]
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
This completes.
** ▼ Procedure ** ① Make each line one list ② Convert to table ③ Change the column name ④ Specify the column that will be the row name
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
df = pd.DataFrame(list)
df
Row and column names will be automatically assigned numbers.
-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
Specify the column that will be the row name
df = df.set_index('')
df
The column name "''" was set in the heading.
Check with the following 3 patterns. (1) Combine each list together (2) Combine each list one by one (3) Combine with mathematical formula
** ▼ Procedure ** ① Create a list for each line ② Convert to a table at once ③ Change the line name ④ Change column name
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 to a table at once
pd.DataFrame([listA,listB,listC,listD,listE,listF])
-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
-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
This completes.
** ▼ Procedure ** ① Create a list for each line ② Create a table ③ Add a column to the table ④ Change the line name ⑤ Transpose
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
df= pd.DataFrame(listA, columns=['row0'])
df
pd.DataFrame({'A':[1,2,,],'B':[3,4,,],})
└ Describe 'column name': [array]
in ({})
Create with column name ①
pd.DataFrame({'row0':[1, 100, 0.33, 'AAA', 'AAA100']})
Create with column name (multiple columns)
pd.DataFrame({'row0':[1, 100, 0.33, 'AAA', 'AAA100'], 'row1':[2, 200, 0.67, 'BBB', 'BBB200']})
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
Rename line
df.index = ['col0','col1','col2','col3','col4']
df
Transpose
df = df.T
df
This completes.
** ▼ Regularity **
** ▼ Procedure ** ① Create list ② Create a table ③ Add columns to the table using formulas ④ Change the line name
Two for col0 and col3.
create list
listA = [1,2,3,4,5,6]
listD = ['AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF']
Creating a table
df = pd.DataFrame(listA, columns=['col0'])
df
・ 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
Rename line
df.index=['row0','row1','row2','row3','row4','row5']
df
This completes.
** ▼ Procedure ** ① Create a table with a csv file ② Import with read_csv method └ Specify heading column
Read csv file
import pandas as pd
pd.read_csv('~/desktop/test.csv', index_col=0)
This completes.
Click here for a list of options for reading csv files (https://qiita.com/yuta-38/items/e1e890a647e77c7ccaad)
・ 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
Recommended Posts