關於pandas的一些筆記

2022-05-13 14:27:07 字數 1222 閱讀 7197

print(df.loc[1, 'row_labels']) #取行索引為1,列索引為row_labels的資料

8 t1 = df.loc[[1,2],] #

取第一行和第二行,等價於df.loc[[1,2],:]

9 t2 = df.loc[[1,2], ['

row_labels

','count_animalname

']] #

取塊10 t3 = df.iloc[:,1] #

取第一列的資料

11 t4 = df.iloc[[8,9], 1] #

取第8行和第9行的第1列資料

12 t5 = df[(df['

count_animalname

']>2)&(df['

count_animalname

']<50)] #

布林值為索引

13 t6 = df[(df['

row_labels

'].str.len()>5)&(df['

count_animalname

']>2)]#

取row_labels字串長度大於5且count_animalname大於2的資料

pands取行或者列的注意點

1、方括號寫陣列,表示取行,對行進行操作 df[:20]

2、寫字串,表示的取列索引,對列進行操作 df['row_labels']

3、df[:100]['row_labels']表示先取前100行,再去row_labels這列的元素

4、df.loc通過行標籤取行資料,df.iloc通過行索引獲取行資料。標籤和索引有區別,索引只能為數字(0,1,2,3...),標籤可以為任意(例如:a,b,c,d...)

關於pandas的一些使用

該函式只對dataframe或series型別有效,用於去除重複值。引數有三個 舉個例子 dataframe pd.read excel data.xlsx dataframe2 pd.read excel data.xlsx dataframe.drop duplicates subset non...

pandas的一些用法

讀取以 t為分隔符的不帶volume的資料names a b c d e f g df pd.read table filename sep t names names 刪除df中某一行df.drop labels none axis 0,index none columns none level ...

pandas的一些誤區

data frame pd.dataframe data in 13 frame.iloc 6 1,asf 201 這一行會報錯indexerror single positional indexer is out of bounds in 14 frame.loc 6 1,hh 612 正確新增一...