選擇某行元素
參考資料
選擇某行或某列元素
有三種思路:
- 依據屬性名
- 利用行號,或者列號
- 依據條件構建boolear variable
import pandas as pd
import numpy as np
df = pd.dataframe(data=np.random.randint(low=1,high=4,size=(2,3)), columns=["a","b","c"])
得到的是乙個series物件
依據屬性名選擇
acol = df['a'] ##選擇a屬性列
columnname = acol.name
依據下標選擇
acol=df.iloc[:,0]
利用屬性名
語法:multicol = df[list]
abcol = df[["a","b"]] ##wayi: list
abcol = df.loc[:, ["a","b"]] ##wayii, 利用loc
利用下標
abcol = df.iloc[:, [0,1]] ##wayiii, 利用iloc
abcol = df.iloc[:, [0:1]] ##wayiv, 利用iloc
核心思想:依據行號選擇
r1 =df[0:1]
核心思路:先構建條件,得到boolean變數
stepi: 構建條件
condition1 = df["a"]==1
condition2 = df["b"]>=2
conditoon3 = condition1 & condition2
stepii:依據條件選擇
subdf = df[condition1]
subdf = df[condition3]
numpy中選擇某行某列
今天看到某個 中有這個形式,查閱資料才懂了什麼意思 import numpy as np array np.array 1,2,3,4 5,6,7,8 9,10,11,12 13,14,15,16 陣列一定要通過np形成,不然會報錯 a array print a 1 2 3 4 5 6 7 8 9...
獲取某行某列的值
獲取某行某列的值 迴圈方式 param row 行數 param col 列數 return number function getval row,col tmp arr for i 1 i row i else return tmp arr row col echo getval 6,2 獲取某行...
獲取某行某列的值
獲取某行某列的值 迴圈方式 param row 行數 param col 列數 return number function getval row,col tmp arr for i 1 i row i else return tmp arr row col echo getval 6,2 獲取某行...