Pandas学习笔记-23_数据列操作

1. Excel,与上一节同

2. Pandas

import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

students = pd.concat([page_001,page_002],axis=1)
    
print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID         Name  Score  ID         Name  Score
0    1  Student_001     90  21  Student_021     80
1    2  Student_002     90  22  Student_022     80
2    3  Student_003     90  23  Student_023     80
3    4  Student_004     90  24  Student_024     80
4    5  Student_005     90  25  Student_025     80
5    6  Student_006     90  26  Student_026     80
6    7  Student_007     90  27  Student_027     80
7    8  Student_008     90  28  Student_028     80
8    9  Student_009     90  29  Student_029     80
9   10  Student_010     90  30  Student_030     80
10  11  Student_011     90  31  Student_031     80
11  12  Student_012     90  32  Student_032     80
12  13  Student_013     90  33  Student_033     80
13  14  Student_014     90  34  Student_034     80
14  15  Student_015     90  35  Student_035     80
15  16  Student_016     90  36  Student_036     80
16  17  Student_017     90  37  Student_037     80
17  18  Student_018     90  38  Student_038     80
18  19  Student_019     90  39  Student_039     80
19  20  Student_020     90  40  Student_040     80
import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002])   
 
print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID         Name  Score
0    1  Student_001     90
1    2  Student_002     90
2    3  Student_003     90
3    4  Student_004     90
4    5  Student_005     90
5    6  Student_006     90
6    7  Student_007     90
7    8  Student_008     90
8    9  Student_009     90
9   10  Student_010     90
10  11  Student_011     90
11  12  Student_012     90
12  13  Student_013     90
13  14  Student_014     90
14  15  Student_015     90
15  16  Student_016     90
16  17  Student_017     90
17  18  Student_018     90
18  19  Student_019     90
19  20  Student_020     90
0   21  Student_021     80
1   22  Student_022     80
2   23  Student_023     80
3   24  Student_024     80
4   25  Student_025     80
5   26  Student_026     80
6   27  Student_027     80
7   28  Student_028     80
8   29  Student_029     80
9   30  Student_030     80
10  31  Student_031     80
11  32  Student_032     80
12  33  Student_033     80
13  34  Student_034     80
14  35  Student_035     80
15  36  Student_036     80
16  37  Student_037     80
17  38  Student_038     80
18  39  Student_039     80
19  40  Student_040     80
import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
 
print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID         Name  Score
0    1  Student_001     90
1    2  Student_002     90
2    3  Student_003     90
3    4  Student_004     90
4    5  Student_005     90
5    6  Student_006     90
6    7  Student_007     90
7    8  Student_008     90
8    9  Student_009     90
9   10  Student_010     90
10  11  Student_011     90
11  12  Student_012     90
12  13  Student_013     90
13  14  Student_014     90
14  15  Student_015     90
15  16  Student_016     90
16  17  Student_017     90
17  18  Student_018     90
18  19  Student_019     90
19  20  Student_020     90
20  21  Student_021     80
21  22  Student_022     80
22  23  Student_023     80
23  24  Student_024     80
24  25  Student_025     80
25  26  Student_026     80
26  27  Student_027     80
27  28  Student_028     80
28  29  Student_029     80
29  30  Student_030     80
30  31  Student_031     80
31  32  Student_032     80
32  33  Student_033     80
33  34  Student_034     80
34  35  Student_035     80
35  36  Student_036     80
36  37  Student_037     80
37  38  Student_038     80
38  39  Student_039     80
39  40  Student_040     80

New Add Column

import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
students['Age']=np.repeat(25,len(students))
print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID         Name  Score  Age
0    1  Student_001     90   25
1    2  Student_002     90   25
2    3  Student_003     90   25
3    4  Student_004     90   25
4    5  Student_005     90   25
5    6  Student_006     90   25
6    7  Student_007     90   25
7    8  Student_008     90   25
8    9  Student_009     90   25
9   10  Student_010     90   25
10  11  Student_011     90   25
11  12  Student_012     90   25
12  13  Student_013     90   25
13  14  Student_014     90   25
14  15  Student_015     90   25
15  16  Student_016     90   25
16  17  Student_017     90   25
17  18  Student_018     90   25
18  19  Student_019     90   25
19  20  Student_020     90   25
20  21  Student_021     80   25
21  22  Student_022     80   25
22  23  Student_023     80   25
23  24  Student_024     80   25
24  25  Student_025     80   25
25  26  Student_026     80   25
26  27  Student_027     80   25
27  28  Student_028     80   25
28  29  Student_029     80   25
29  30  Student_030     80   25
30  31  Student_031     80   25
31  32  Student_032     80   25
32  33  Student_033     80   25
33  34  Student_034     80   25
34  35  Student_035     80   25
35  36  Student_036     80   25
36  37  Student_037     80   25
37  38  Student_038     80   25
38  39  Student_039     80   25
39  40  Student_040     80   25
import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
# students['Age']=np.repeat(25,len(students))
students['Age']=np.arange(0,len(students))
print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID         Name  Score  Age
0    1  Student_001     90    0
1    2  Student_002     90    1
2    3  Student_003     90    2
3    4  Student_004     90    3
4    5  Student_005     90    4
5    6  Student_006     90    5
6    7  Student_007     90    6
7    8  Student_008     90    7
8    9  Student_009     90    8
9   10  Student_010     90    9
10  11  Student_011     90   10
11  12  Student_012     90   11
12  13  Student_013     90   12
13  14  Student_014     90   13
14  15  Student_015     90   14
15  16  Student_016     90   15
16  17  Student_017     90   16
17  18  Student_018     90   17
18  19  Student_019     90   18
19  20  Student_020     90   19
20  21  Student_021     80   20
21  22  Student_022     80   21
22  23  Student_023     80   22
23  24  Student_024     80   23
24  25  Student_025     80   24
25  26  Student_026     80   25
26  27  Student_027     80   26
27  28  Student_028     80   27
28  29  Student_029     80   28
29  30  Student_030     80   29
30  31  Student_031     80   30
31  32  Student_032     80   31
32  33  Student_033     80   32
33  34  Student_034     80   33
34  35  Student_035     80   34
35  36  Student_036     80   35
36  37  Student_037     80   36
37  38  Student_038     80   37
38  39  Student_039     80   38
39  40  Student_040     80   39

Delete Column

import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
# students['Age']=np.repeat(25,len(students))
students['Age']=np.arange(0,len(students))
students.drop(columns=['Age','Score'],inplace=True)

print(students)

runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID         Name
0    1  Student_001
1    2  Student_002
2    3  Student_003
3    4  Student_004
4    5  Student_005
5    6  Student_006
6    7  Student_007
7    8  Student_008
8    9  Student_009
9   10  Student_010
10  11  Student_011
11  12  Student_012
12  13  Student_013
13  14  Student_014
14  15  Student_015
15  16  Student_016
16  17  Student_017
17  18  Student_018
18  19  Student_019
19  20  Student_020
20  21  Student_021
21  22  Student_022
22  23  Student_023
23  24  Student_024
24  25  Student_025
25  26  Student_026
26  27  Student_027
27  28  Student_028
28  29  Student_029
29  30  Student_030
30  31  Student_031
31  32  Student_032
32  33  Student_033
33  34  Student_034
34  35  Student_035
35  36  Student_036
36  37  Student_037
37  38  Student_038
38  39  Student_039
39  40  Student_040

Insert column

import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
# students['Age']=np.repeat(25,len(students))
students['Age']=np.arange(0,len(students))
students.drop(columns=['Age','Score'],inplace=True)
students.insert(1,column='Foo',value=np.repeat('foo',len(students)))

print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID  Foo         Name
0    1  foo  Student_001
1    2  foo  Student_002
2    3  foo  Student_003
3    4  foo  Student_004
4    5  foo  Student_005
5    6  foo  Student_006
6    7  foo  Student_007
7    8  foo  Student_008
8    9  foo  Student_009
9   10  foo  Student_010
10  11  foo  Student_011
11  12  foo  Student_012
12  13  foo  Student_013
13  14  foo  Student_014
14  15  foo  Student_015
15  16  foo  Student_016
16  17  foo  Student_017
17  18  foo  Student_018
18  19  foo  Student_019
19  20  foo  Student_020
20  21  foo  Student_021
21  22  foo  Student_022
22  23  foo  Student_023
23  24  foo  Student_024
24  25  foo  Student_025
25  26  foo  Student_026
26  27  foo  Student_027
27  28  foo  Student_028
28  29  foo  Student_029
29  30  foo  Student_030
30  31  foo  Student_031
31  32  foo  Student_032
32  33  foo  Student_033
33  34  foo  Student_034
34  35  foo  Student_035
35  36  foo  Student_036
36  37  foo  Student_037
37  38  foo  Student_038
38  39  foo  Student_039
39  40  foo  Student_040

Update title

import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
# students['Age']=np.repeat(25,len(students))
students['Age']=np.arange(0,len(students))
students.drop(columns=['Age','Score'],inplace=True)
students.insert(1,column='Foo',value=np.repeat('foo',len(students)))
students.rename(columns={'Foo':'FOO','Name':'NAME'},inplace=True)

print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
    ID  FOO         NAME
0    1  foo  Student_001
1    2  foo  Student_002
2    3  foo  Student_003
3    4  foo  Student_004
4    5  foo  Student_005
5    6  foo  Student_006
6    7  foo  Student_007
7    8  foo  Student_008
8    9  foo  Student_009
9   10  foo  Student_010
10  11  foo  Student_011
11  12  foo  Student_012
12  13  foo  Student_013
13  14  foo  Student_014
14  15  foo  Student_015
15  16  foo  Student_016
16  17  foo  Student_017
17  18  foo  Student_018
18  19  foo  Student_019
19  20  foo  Student_020
20  21  foo  Student_021
21  22  foo  Student_022
22  23  foo  Student_023
23  24  foo  Student_024
24  25  foo  Student_025
25  26  foo  Student_026
26  27  foo  Student_027
27  28  foo  Student_028
28  29  foo  Student_029
29  30  foo  Student_030
30  31  foo  Student_031
31  32  foo  Student_032
32  33  foo  Student_033
33  34  foo  Student_034
34  35  foo  Student_035
35  36  foo  Student_036
36  37  foo  Student_037
37  38  foo  Student_038
38  39  foo  Student_039
39  40  foo  Student_040
import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
# students['Age']=np.repeat(25,len(students))
students['Age']=np.arange(0,len(students))
students.drop(columns=['Age','Score'],inplace=True)
students.insert(1,column='Foo',value=np.repeat('foo',len(students)))
students.rename(columns={'Foo':'FOO','Name':'NAME'},inplace=True)

students['ID']=students['ID'].astype(float)
for i in range(5,15):
    students['ID'].at[i] = np.nan

print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
      ID  FOO         NAME
0    1.0  foo  Student_001
1    2.0  foo  Student_002
2    3.0  foo  Student_003
3    4.0  foo  Student_004
4    5.0  foo  Student_005
5    NaN  foo  Student_006
6    NaN  foo  Student_007
7    NaN  foo  Student_008
8    NaN  foo  Student_009
9    NaN  foo  Student_010
10   NaN  foo  Student_011
11   NaN  foo  Student_012
12   NaN  foo  Student_013
13   NaN  foo  Student_014
14   NaN  foo  Student_015
15  16.0  foo  Student_016
16  17.0  foo  Student_017
17  18.0  foo  Student_018
18  19.0  foo  Student_019
19  20.0  foo  Student_020
20  21.0  foo  Student_021
21  22.0  foo  Student_022
22  23.0  foo  Student_023
23  24.0  foo  Student_024
24  25.0  foo  Student_025
25  26.0  foo  Student_026
26  27.0  foo  Student_027
27  28.0  foo  Student_028
28  29.0  foo  Student_029
29  30.0  foo  Student_030
30  31.0  foo  Student_031
31  32.0  foo  Student_032
32  33.0  foo  Student_033
33  34.0  foo  Student_034
34  35.0  foo  Student_035
35  36.0  foo  Student_036
36  37.0  foo  Student_037
37  38.0  foo  Student_038
38  39.0  foo  Student_039
39  40.0  foo  Student_040
import pandas as pd
import numpy as np

page_001 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_001')
page_002 = pd.read_excel('C:/Tools/Python/Pandas/027/Students.xlsx',sheet_name='Page_002')

# students = pd.concat([page_001,page_002],axis=1)
students = pd.concat([page_001,page_002]).reset_index(drop=True)   
#students['Age']=25 
# students['Age']=np.repeat(25,len(students))
students['Age']=np.arange(0,len(students))
students.drop(columns=['Age','Score'],inplace=True)
students.insert(1,column='Foo',value=np.repeat('foo',len(students)))
students.rename(columns={'Foo':'FOO','Name':'NAME'},inplace=True)

students['ID']=students['ID'].astype(float)
for i in range(5,15):
    students['ID'].at[i] = np.nan
    
students.dropna(inplace=True)
    
print(students)
runfile('C:/Tools/Python/python资料/00-1903XLY/day04/day04/code/pandasDemo20.py', wdir='C:/Tools/Python/python资料/00-1903XLY/day04/day04/code')
      ID  FOO         NAME
0    1.0  foo  Student_001
1    2.0  foo  Student_002
2    3.0  foo  Student_003
3    4.0  foo  Student_004
4    5.0  foo  Student_005
15  16.0  foo  Student_016
16  17.0  foo  Student_017
17  18.0  foo  Student_018
18  19.0  foo  Student_019
19  20.0  foo  Student_020
20  21.0  foo  Student_021
21  22.0  foo  Student_022
22  23.0  foo  Student_023
23  24.0  foo  Student_024
24  25.0  foo  Student_025
25  26.0  foo  Student_026
26  27.0  foo  Student_027
27  28.0  foo  Student_028
28  29.0  foo  Student_029
29  30.0  foo  Student_030
30  31.0  foo  Student_031
31  32.0  foo  Student_032
32  33.0  foo  Student_033
33  34.0  foo  Student_034
34  35.0  foo  Student_035
35  36.0  foo  Student_036
36  37.0  foo  Student_037
37  38.0  foo  Student_038
38  39.0  foo  Student_039
39  40.0  foo  Student_040
发布了83 篇原创文章 · 获赞 43 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/f2157120/article/details/104059947