1.df.pop() 删除某一列
可以使用这个来删除某一列(不能是多列),只有一个参数,就是列名,可以是str类型,函数返回的是被删除的列,df直接是删除后的df,不需要我们处理。
我们建模时,需要单独保留某个特征,比如y值,我们就可以用y=xx.pop(label)了,很实用。
实例
(py3.7) C:\Users\HASEE>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
... ('parrot', 'bird', 24.0),
... ('lion', 'mammal', 80.5),
... ('monkey', 'mammal', np.nan)],
... columns=('name', 'class', 'max_speed'))
>>> df
name class max_speed
0 falcon bird 389.0
1 parrot bird 24.0
2 lion mammal 80.5
3 monkey mammal NaN
>>> df.pop('class') # 返回值直接是删除的某列的内容
0 bird
1 bird
2 mammal
3 mammal
Name: class, dtype: object
>>> df # 删除class列之后的df
name max_speed
0 falcon 389.0
1 parrot 24.0
2 lion 80.5
3 monkey NaN
内容参考自:https://www.cnblogs.com/cgmcoding/p/13729825.html
2. np.array.shape()
numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数。有时候我们可能需要知道某一维的特定维数。
(python3.7) C:\Users\HASEE>python
Python 3.7.16 (default, Jan 17 2023, 16:06:28) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> # 二维
>>> y=np.array([[1, 2, 3], [4, 5, 6]])
>>> print(y)
[[1 2 3]
[4 5 6]]
>>> print(y.shape)
(2, 3)
>>> print(y.shape[0])
2
>>> print(y.shape[1])
3
>>>
>>> # 三维
>>> x=np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [0, 1, 2]],[[3, 4, 5], [6, 7, 8]]])
>>> print(x)
[[[1 2 3]
[4 5 6]]
[[7 8 9]
[0 1 2]]
[[3 4 5]
[6 7 8]]]
>>> print(x.shape[0])
3
>>> print(x.shape[1])
2
>>> print(x.shape[2])
3
要点:
y是一个两行三列的二维数组,y.shape[0]代表行数,y.shape[1]代表列数。
x是一个包含了3个两行三列的二维数组的三维数组,x.shape[0]代表包含二维数组的个数,x.shape[1]表示二维数组的行数,x.shape[2]表示二维数组的列数。
shape[0]表示最外围的数组的维数,shape[1]表示次外围的数组的维数,数字不断增大,维数由外到内。