About the basic operation of creating a table in python. A summary of how to create and operate a table based on one list data for easy understanding.
Use pandas DataFrame to create the table.
listA = ['AAA', 'BBB', 'CCC', 'DDD', 'EEE']
pd.DataFrame (array)
└ "pd": pandas
Abbreviation. pd import
└ "DataFrame": 2D table data
Creating a table
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1 = pd.DataFrame(listA)
df1
#output
0
0 AAA
1 BBB
2 CCC
3 DDD
-The array is output as a table ・ The first column is the heading (index number from 0) ・ The first row is the column name (index number from 0)
Creating a table (directly)
import pandas as pd
pd.DataFrame(['AAA', 'BBB', 'CCC', 'DDD'])
#output
0
0 AAA
1 BBB
2 CCC
3 DDD
Specify columns = ['AAA']
in the DataFrame option.
└ "AAA": Column name (optional)
Rename column
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df2 = pd.DataFrame(listA, columns=['Ah ah'])
df2
#output
Ah ah
0 AAA
1 BBB
2 CCC
3 DDD
df.columns = ['AAA']
└ "df": Table data
└ ".columns": Get column names
└ "AAA": Column name to be assigned
Assign using the columns method.
Rename column (later)
df1.columns = ['Ah ah']
df1
#output
Ah ah
0 AAA
1 BBB
2 CCC
3 DDD
rename can specify which column name to change and how.
{Original column name: Modified column name}
Change with rename method
df1.rename(columns={0:'Ah ah'})
#output
Ah ah
0 AAA
1 BBB
2 CCC
3 DDD
① Designated when creating the table ② Change later ②-1. index method ②-2. rename method
Specify ʻindex = ['AAA','BBB' ,,,,]` in the DataFrame option. └ "AAA" "BBB": Line name (optional) └ Either character string or numerical value can be specified
** ▼ Specify by character string **
Rename column (character string)
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1 = pd.DataFrame(listA, index=['111','222','333','444'])
df1
#output
0
111 AAA
222 BBB
333 CCC
444 DDD
Column name change (numerical value)
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1 = pd.DataFrame(listA, index=[111,22.2,3.33,444])
df1
#output
0
111.00 AAA
22.20 BBB
3.33 CCC
444.00 DDD
Column name change (numerical value)
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
indexA = ['111','222','333','444']
df1 = pd.DataFrame(listA, index=indexA)
df1
#output
0
111 AAA
222 BBB
333 CCC
444 DDD
Specify df.index = ['AAA','BBB' ,,,,]
.
└ "AAA" "BBB": Line name (optional)
└ Both character string and numerical value can be specified
Change index name later
df1.index = ['111','222','333','444']
df1
#output
0
111 AAA
222 BBB
333 CCC
444 DDD
rename can specify which column name to change and how.
rename = (index = {original line name: changed line name})
Change with rename method
df1.rename(index={1:'111',3:'333'})
#output
0
0 AAA
111 BBB
2 CCC
333 DDD
① Designated when creating the table ② Change later ②-1. index method ②-2. rename method
With DataFrame options
columns=['AAA','BBB',,,,]
index=['aaa','bbb',,,,]
Is specified.
└ "AAA" "BBB": Column name (optional)
└ "aaa" "bbb": Line name (optional)
Specify a matrix
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1 = pd.DataFrame(listA, columns=['Ah ah'], index=['111','222','333','444'])
df1
#output
Ah ah
111 AAA
222 BBB
333 CCC
444 DDD
Specify a matrix (variable)
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
cols = ['Ah ah']
inds = ['111','222','333','444']
df1 = pd.DataFrame(listA, columns=cols, index=inds)
df1
#output
Ah ah
111 AAA
222 BBB
333 CCC
444 DDD
rename can specify which column name to change and how.
rename (index = {original row name: modified row name}, columns = {original column name: modified column name})
Specify matrix (rename method)
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1 = pd.DataFrame(listA)
df1.rename(columns={0:'Good'}, index={1:'111', 3:'333', 4:'444'})
#output
Good
0 AAA
111 BBB
2 CCC
333 DDD
Create a multi-column table from multiple lists.
Combine two lists.
list
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
Method ① Combine in the state of list ② Make a table and then join
df['aaa'] = ['AAA', 'BBB',,,]
└ "df": Original table
└ "aaa": Column name to be added (numerical value is also acceptable)
└ "AAA" "BBB": Column elements
Add column
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1= pd.DataFrame(listA)
df1[1] = ['EEE', 'FFF', 'GGG', 'HHH']
df1
#output
0 1
0 AAA EEE
1 BBB FFF
2 CCC GGG
3 DDD HHH
Error if the number of elements is different
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
df1= pd.DataFrame(listA)
df1[1] = ['EEE', 'FFF', 'GGG']
df1
#output
# ValueError: Length of values does not match length of index
df['aaa']
= dfB
└ "df": Original table
└ "aaa": Column name to be added (numerical value is also acceptable)
└ "AAA" "BBB": Column elementsAdd column
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
dfA= pd.DataFrame(listA)
listB =['EEE', 'FFF', 'GGG', 'HHH']
dfB= pd.DataFrame(listB)
dfA['1'] = dfB
dfA
#output
0 1
0 AAA EEE
1 BBB FFF
2 CCC GGG
3 DDD HHH
In the case of "+"
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
dfA= pd.DataFrame(listA)
listB =['EEE', 'FFF', 'GGG', 'HHH']
dfB= pd.DataFrame(listB)
dfA + dfB
#output
0
0 AAAEEE
1 BBBFFF
2 CCCGGG
3 DDDHHH
Original table
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
listC = ['III', 'JJJ', 'KKK', 'LLL']
listD = ['MMM', 'NNN', 'OOO', 'PPP']
Join table
import pandas as pd
listA = ['AAA', 'BBB', 'CCC', 'DDD']
listB = ['EEE', 'FFF', 'GGG', 'HHH']
listC = ['III', 'JJJ', 'KKK', 'LLL']
listD = ['MMM', 'NNN', 'OOO', 'PPP']
dfA = pd.DataFrame(listA)
dfA[1] = listB
dfA[2] = listC
dfA[3] = listD
dfA
#output
0 1 2 3
0 AAA EEE III MMM
1 BBB FFF JJJ NNN
2 CCC GGG KKK OOO
3 DDD HHH LLL PPP