一、numpy篇
1.一维数据
基本形式:X[i:], X[i],X[i,ii]
与C语言类似,这里重点讲第一个,X[i:]表示提取第(i-1)个数据往后的所有数据,不包括第(i-1)个数据。(如果i为负数,则是第i个数据往前的所有数据,不包括第i个)
举例:
xy =[1, 2, 3, 4, 5]
x = xy[2:]
y = xy[:-2]
print(x, y)
>>>
[3, 4, 5] [1, 2, 3]
2.二维数据
基本形式:
X[:,i]、 X[:,m:n]、 X[a:b,m:n]
(1)X[:,i] 表示一个二维数组的所有行,但是只选择第i列的数据
(2)X[:,m:n] 表示一个二维数组的所有行,但是只选择第m列到第(n-1)列的数据
(3) X[a:b,m:n] 表示一个二维数组的a行到(b-1)行,且选择第m列到第(n-1)列的数据
示例如下:
import numpy as np
b = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
b1 = b[:, 2]
b2 = b[:, 0:1]
b3 = b[0:1,0:1]
print('b1:', b1)
print('b2:', b2)
print('b3:', b3)
>>>
b1: [3 6 9]
b2: [[1]
[4]
[7]]
b3: [[1]]
3.三维及以上数据
基本形式:X[:,:,i]、 X[:,i,:]、 X[:,:,m:n]
(1) X[:,:,i] 表示选取三个维度的数组内矩阵的所有行,但是只选择矩阵第i列的数据
(2)X[:,i,:] 表示选取三个维度的数组内矩阵的第i行,且包含这些行内所有列的数据
(3) X[:,:,m:n] 表示选取三个维度的数组内矩阵的所有行,且选择矩阵第m列到第(n-1)列的数据
示例如下:
import numpy as np
data_list = [[[1, 2], [1, 0], [3, 4], [7, 9], [4, 0]],
[[1, 4], [1, 5], [3, 6], [8, 9], [5, 0]],
[[8, 2], [1, 8], [3, 5], [7, 3], [4, 6]]]
data_list = np.array(data_list)
print(data_list[:, :, 0], '\n')
print(data_list[:, 1, :], '\n')
print(data_list[:, :, 0:1])
>>>
[[1 1 3 7 4]
[1 1 3 8 5]
[8 1 3 7 4]]
[[1 0]
[1 5]
[1 8]]
[[[1]
[1]
[3]
[7]
[4]]
[[1]
[1]
[3]
[8]
[5]]
[[8]
[1]
[3]
[7]
[4]]]
4.注意,切片操作可以互相嵌入,如下代码中x_data所示:
import numpy as np
xy = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
]
xy = np.array(xy)
y_data = xy[:,:-1]
x_data = xy[:,:-1][:,:-1]
print(y_data)
print(x_data)
>>>
[[ 1 2 3 4]
[ 6 7 8 9]
[11 12 13 14]
[16 17 18 19]
[21 22 23 24]]
[[ 1 2 3]
[ 6 7 8]
[11 12 13]
[16 17 18]
[21 22 23]]
二、pandas篇
主要使用pandas包的iloc函数返回一个伪切片类的数据结构,实际上是字典,可以用values()函数进一步提取里面的数据值,提取后数据类型为ndarray,对于一维二维三维数据的处理与numpy类似,这里不再赘述。
iloc介绍如下:
iloc中文介绍可以参照这个博主:
关于lamda函数的使用示例和解释可以参考这位博主的文章:python中lambda函数if用法-Python中关于Lambda函数的使用总结-CSDN博客
以下是示例:
mydict = [{'a': 1, 'b': 2, 'c': 3, 'd': 4},
{'a': 100, 'b': 200, 'c': 300, 'd': 400},
{'a': 1000, 'b': 2000, 'c': 3000, 'd': 4000 }]
df = pd.DataFrame(mydict)
>>> df
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
#Indexing just the rows
#With a scalar integer.
type(df.iloc[0])
<class 'pandas.core.series.Series'>
>>> df.iloc[0]
a 1
b 2
c 3
d 4
#Name: 0, dtype: int64
#With a list of integers.
>>> df.iloc[[0]]
a b c d
0 1 2 3 4
>>> type(df.iloc[[0]])
<class 'pandas.core.frame.DataFrame'>
>>> df.iloc[[0, 1]]
a b c d
0 1 2 3 4
1 100 200 300 400
#With a slice object.
>>> df.iloc[:3]
a b c d
0 1 2 3 4
1 100 200 300 400
2 1000 2000 3000 4000
#With a boolean mask the same length as the index.
>>> df.iloc[[True, False, True]]
a b c d
0 1 2 3 4
2 1000 2000 3000 4000
#With a callable, useful in method chains. The x passed to the lambda is the DataFrame #being sliced. This selects the rows whose index label even.
>>> df.iloc[lambda x: x.index % 2 == 0]
a b c d
0 1 2 3 4
2 1000 2000 3000 4000
#Indexing both axes
#You can mix the indexer types for the index and columns. Use : to select the entire axis.
#With scalar integers.
>>> df.iloc[0, 1]
2
#With lists of integers.
>>> df.iloc[[0, 2], [1, 3]]
b d
0 2 4
2 2000 4000
#With slice objects.
>>> df.iloc[1:3, 0:3]
a b c
1 100 200 300
2 1000 2000 3000
#With a boolean array whose length matches the columns.
>>> df.iloc[:, [True, False, True, False]]
a c
0 1 3
1 100 300
2 1000 3000
#With a callable function that expects the Series or DataFrame.
>>> df.iloc[:, lambda df: [0, 2]]
a c
0 1 3
1 100 300
2 1000 3000