表1

表2

把两个表横向排列(通过axis=1实现)
import pandas as pd
df_1 = pd.read_excel(io='exls/columns.xlsx', sheet_name='Sheet1')
df_2 = pd.read_excel(io='exls/columns.xlsx', sheet_name='Sheet2')
# 把两个表横向排列(通过axis=1实现)
df_new = pd.concat([df_1, df_2], axis=1).reset_index(drop=True)
print(df_new)
output
ID NAME SCORES ID NAME SCORES
0 1 S1 71 11 S11 61
1 2 S2 72 12 S12 62
2 3 S3 73 13 S13 63
3 4 S4 74 14 S14 64
4 5 S5 75 15 S15 65
5 6 S6 76 16 S16 66
6 7 S7 77 17 S17 67
7 8 S8 78 18 S18 68
8 9 S9 79 19 S19 69
9 10 S10 80 20 S20 70
实际工作中意义不大
添加一列
df_new = pd.concat([df_1, df_2]).reset_index(drop=True)
# 添加一列
df_new['AGE'] = 25
output
ID NAME SCORES AGE
0 1 S1 71 25
1 2 S2 72 25
2 3 S3 73 25
3 4 S4 74 25
4 5 S5 75 25
5 6 S6 76 25
6 7 S7 77 25
7 8 S8 78 25
8 9 S9 79 25
9 10 S10 80 25
10 11 S11 61 25
11 12 S12 62 25
12 13 S13 63 25
13 14 S14 64 25
14 15 S15 65 25
15 16 S16 66 25
16 17 S17 67 25
17 18 S18 68 25
18 19 S19 69 25
19 20 S20 70 25
添加一个序列
import numpy as np
# 添加一个序列
df_new['Series'] = np.arange(0, len(df_new))
output
ID NAME SCORES AGE Series
0 1 S1 71 25 0
1 2 S2 72 25 1
2 3 S3 73 25 2
3 4 S4 74 25 3
4 5 S5 75 25 4
5 6 S6 76 25 5
6 7 S7 77 25 6
7 8 S8 78 25 7
8 9 S9 79 25 8
9 10 S10 80 25 9
10 11 S11 61 25 10
11 12 S12 62 25 11
12 13 S13 63 25 12
13 14 S14 64 25 13
14 15 S15 65 25 14
15 16 S16 66 25 15
16 17 S17 67 25 16
17 18 S18 68 25 17
18 19 S19 69 25 18
19 20 S20 70 25 19
删除列
df_new.drop(columns=['AGE', 'SCORES'], inplace=True)
output
ID NAME Series
0 1 S1 0
1 2 S2 1
2 3 S3 2
3 4 S4 3
4 5 S5 4
5 6 S6 5
6 7 S7 6
7 8 S8 7
8 9 S9 8
9 10 S10 9
10 11 S11 10
11 12 S12 11
12 13 S13 12
13 14 S14 13
14 15 S15 14
15 16 S16 15
16 17 S17 16
17 18 S18 17
18 19 S19 18
19 20 S20 19
插入列
df_new.insert(1, column='INSERT', value=np.repeat('insert', len(df_new)))
output
ID INSERT NAME Series
0 1 insert S1 0
1 2 insert S2 1
2 3 insert S3 2
3 4 insert S4 3
4 5 insert S5 4
5 6 insert S6 5
6 7 insert S7 6
7 8 insert S8 7
8 9 insert S9 8
9 10 insert S10 9
10 11 insert S11 10
11 12 insert S12 11
12 13 insert S13 12
13 14 insert S14 13
14 15 insert S15 14
15 16 insert S16 15
16 17 insert S17 16
17 18 insert S18 17
18 19 insert S19 18
19 20 insert S20 19
修改列名
df_new.rename(columns={'INSERT': 'Insert', 'NAME': 'Name'}, inplace=True)
output
ID Insert Name Series
0 1 insert S1 0
1 2 insert S2 1
2 3 insert S3 2
网友评论