python行列操作集錦

2021-10-09 21:31:40 字數 2085 閱讀 1488

現有乙個excel工作簿,裡面有兩張表student1和student2,表結構一樣,各有三列:學生id,學生名和分數,分別對其進行行列操作,以便於更深刻掌握python關於行列操作的語法。

)#縱向合併**

.reset_index(drop=

true

)#在末尾新增行

stu=pd.series(

)true

)#在中間插入行

stu1=student[:19

]stu2=student[19:

]stu=pd.series(

)true

).reset_index(

)#更改行資料

#方法1

student.at[23,

"student"]=

"lily"

student.at[23,

"score"]=

100#方法2

stu1=pd.series(

)student.iloc[22]

=stu1

#刪除行

student.drop(index=student[:10

],inplace=

true

)#刪除空白行

missing=student.loc[student[

"student"]==

""]student.drop(index=missing.index,inplace=

true

)student=student.reset_index(drop=

true

)

import pandas as pd

import numpy as np

student1=pd.read_excel(r"c:\users\desktop\example.xlsx"

,sheet_name=

"student1"

)student2=pd.read_excel(r"c:\users\desktop\example.xlsx"

,sheet_name=

"student2"

)#橫向合併兩張表

student=pd.concat(

[student1,student2]

,axis=1)

#縱向合併兩張表

student=pd.concat(

[student1,student2]

).reset_index(drop=

true

)#追加列

student[

"age"

]=np.arange(0,

len(student)

)student[

"w"]=90

#刪除列

student.drop(columns=

["age"

,"w"

],inplace=

true

)#插入列

student.insert(

2,column=

"age"

,value=25)

#列序列從0開始計數,2指新列插在序號為2的列(第3列)前面

#改列名

student.rename(columns=

,inplace=

true

)

python陣列的行列操作

coding utf 8 import numpy as np listdata 1,1,2 1,2,2 1,3,2 1,4,2 newlistdata np.array listdata print 原陣列 print newlistdata print print 取第一行 newlistdat...

pymongo 操作集錦

匯入模組 import pymongo 建立mongoclient連線,需先啟動已經安裝的mongodb服務 client mongoclient localhost 27017 或者 client mongoclient mongodb localhost 27017 進入資料庫 db clien...

介面操作集錦

1,table 中增加標籤,可以使table中tr載入後即顯示。不需要等到整個table載入結束。2,控制文字框中英文大寫,可以通過樣式來控制。在樣式中增加 font variant small caps 即可。3,在table屬性上增加word break break all table layo...