Nump,Pandas使用

  • 扁平化 ravel flatten aqueeze reshape(-1) 的区别

squeeze : 将维数为1的删掉

a= np.arange(6.).reshape((2,3))
print(a.squeeze())
print(a)

[[0. 1. 2.]
 [3. 4. 5.]]
[[0. 1. 2.]
 [3. 4. 5.]]

a= np.arange(6.).reshape((6,1))  # 或 reshape((1,6)) 
print(a.squeeze())
print(a)

[0. 1. 2. 3. 4. 5.]
[[0.]
 [1.]
 [2.]
 [3.]
 [4.]
 [5.]]
import numpy as np
a= np.arange(6.).reshape((1,6,1))

print(a.squeeze())

[ 0.  1.  2.  3.  4.  5.]

flatten()返回一份拷贝,对拷贝所做的修改不会影响原始矩阵,而ravel()返回的是视图,会影响原始矩阵。

import numpy as np
a= np.arange(6.).reshape((2,3))

print(a.flatten())
print('\n')

print(a.ravel())

[ 0.  1.  2.  3.  4.  5.]


[ 0.  1.  2.  3.  4.  5.]
  • DataFrame中添加一行
import pandas as pd

df = pd.DataFrame({'name':['tom','jack'],'age':[50,18],'sex':['man','woman']})

df.loc[2]=[8,9,6]

print(df)

  • ndarray 转 list
    a为ndarray
    b = a.tolist()
    b为列表

  • 仅取出特定列

data = pd.read_csv('qq.csv',usecols=['admitted','exam2'])

猜你喜欢

转载自blog.csdn.net/baidu_41867252/article/details/88686464