Overview:
The processing of the data table is mainly to transpose the rows and columns of the data in the data table, convert the data table into a tree structure, table association, etc.
1. Row and column transposition
Call the T property of the DateFrame object directly:
import pandas as pd
dt=pd.read_excel('产品统计表.xlsx',sheet_name=0)
print(dt)
a=dt.T
print(a)
2. Transform into a tree structure
Use the stack() function
import pandas as pd
dt=pd.read_excel('产品统计表.xlsx',sheet_name=0)
print(dt)
a=dt.stack()
print(a)
3. Splicing of data tables
The splicing of data tables is to merge two or more data tables into one table. The main functions used are merge() and concat()
merge():
import pandas as pd
dt1=pd.read_excel('产品表.xlsx',sheet_name=0)
dt2=pd.read_excel('产品表.xlsx',sheet_name=1)
print(dt1)
print(dt2)
a=pd.merge(dt1,dt2,how='outer',on='员工姓名')
#how默认为inner,如果设置为outer,为外连接,与SQL类似
#on为关联条件,如果不设置,则默认两张表中所有的相同字段
print(a)
concat() function:
import pandas as pd
dt1=pd.read_excel('产品表.xlsx',sheet_name=0)
dt2=pd.read_excel('产品表.xlsx',sheet_name=1)
#print(dt1)
#print(dt2)
a=pd.concat([dt1,dt2],ignore_index=True)
#ignore_index如果不设置,默认为False,区别为结果的首列的序列,True为重置行标签,False为保持两表原来的标签
print(a)
Results of the: