簡談array,reshape,dataframe

array

Numpy的數(shù)據(jù)結(jié)構(gòu)是n維的數(shù)組對象,叫做ndarray。
NumPy數(shù)組一般是同質(zhì)的(但有一種特殊的數(shù)組類型例外,它是異質(zhì)的),即數(shù)組中的所有元素類型必須是一致的。[1]

優(yōu)點:

  • 內(nèi)存塊風(fēng)格:ndarray中的所有元素的類型都是相同的,存儲元素時內(nèi)存可以連續(xù),在科學(xué)計算中,Numpy的ndarray就可以省掉很多循環(huán)語句,代碼使用方面比Python原生list簡單的多。
  • ndarray支持并行化運算(向量化運算)
  • Numpy底層使用C語言編寫,內(nèi)部解除了GIL(全局解釋器鎖),其對數(shù)組的操作速度不受Python解釋器的限制,效率遠(yuǎn)高于純Python代碼。
import numpy as np
x = np.array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])  # 16行,沒有列

y = np.array([[1,2,3,4],[8,6,9,5]])  # 2行4列

print("x行:{}".format(x.shape[0]))
print("x維度:{}".format(x.shape))
print("y行:{}".format(y.shape[0]))
print("y列:{}".format(y.shape[1]))

# out:
# x行:16
# x維度:(16,)
# y行:2
# y列:4

reshape()

官方文檔

numpy.reshape(a, newshape, order='C')[source]

  • a:數(shù)組--需要處理的數(shù)據(jù)[2]
  • newshape:新的格式--整數(shù)或整數(shù)數(shù)組,如(2,3)表示2行3列,新的形狀應(yīng)該與原來的形狀兼容,即行數(shù)和列數(shù)相乘后等于a中元素的數(shù)量
  • order : 可選范圍為{‘C’, ‘F’, ‘A’}。使用索引順序讀取a的元素,并按照索引順序?qū)⒃胤诺阶儞Q后的的數(shù)組中。如果不進行order參數(shù)的設(shè)置,默認(rèn)參數(shù)為C。

(1)“C”指的是用類C寫的讀/索引順序的元素,最后一個維度變化最快,第一個維度變化最慢。以二維數(shù)組為例,簡單來講就是橫著讀,橫著寫,優(yōu)先讀/寫一行。

(2)“F”是指用FORTRAN類索引順序讀/寫元素,最后一個維度變化最慢,第一個維度變化最快。豎著讀,豎著寫,優(yōu)先讀/寫一列。注意,“C”和“F”選項不考慮底層數(shù)組的內(nèi)存布局,只引用索引的順序。

(3)“A”選項所生成的數(shù)組的效果與原數(shù)組a的數(shù)據(jù)存儲方式有關(guān),如果數(shù)據(jù)是按照FORTRAN存儲的話,它的生成效果與”F“相同,否則與“C”相同。這里可能聽起來有點模糊,下面會給出示例。

import numpy as np
x = np.array([[1,2,3,4],[82,63,91,52],[121,345,567,987]])

x1 = x.reshape((2,6),order='C')  # 橫著讀,橫著寫,優(yōu)先讀/寫一行
x2 = x.reshape((2,6),order='F')  # 豎著讀,豎著寫,優(yōu)先讀/寫一列
x3 = x.reshape((2,6),order='A')  # 原數(shù)組FORTRAN存儲,則豎著讀,豎著寫,優(yōu)先讀/寫一列,否則橫著讀,橫著寫,優(yōu)先讀/寫一行

print("x:\n{}\n".format(x))
print("x1:\n{}\n".format(x1))
print("x2:\n{}\n".format(x2))
print("x3:\n{}\n".format(x3))

# out:
# x:
# [[  1   2   3   4]
#  [ 82  63  91  52]
#  [121 345 567 987]]

# x1:
# [[  1   2   3   4  82  63]
#  [ 91  52 121 345 567 987]]

# x2:
# [[  1 121  63   3 567  52]
#  [ 82   2 345  91   4 987]]

# x3:
# [[  1   2   3   4  82  63]
#  [ 91  52 121 345 567 987]]

出現(xiàn)-1的話,有兩種情況

  • reshape(-1):原本數(shù)組有n個元素,返回一個n行無列的數(shù)組
  • reshape(-1,n) n為任意數(shù)字,n為列數(shù),-1會根據(jù)列數(shù),自動計算出新數(shù)組的行數(shù),再根據(jù)這個新的維度重新組合數(shù)組。
x = np.array([[1,2,3,4],[82,63,91,52],[121,345,567,987]])
y = x.reshape(-1)

print("x:\n{}\n".format(x))
print("y:\n{}\n".format(y))  

# out:
# x:
# [[  1   2   3   4]
#  [ 82  63  91  52]
#  [121 345 567 987]]

# y:
# [  1   2   3   4  82  63  91  52 121 345 567 987]
x = np.array([[1,2,3,4],[82,63,91,52],[121,345,567,987]])
y = x.reshape(-1,2)

print("x:\n{}\n".format(x))
print("y:\n{}\n".format(y))  

# out:
# x:
# [[  1   2   3   4]
#  [ 82  63  91  52]
#  [121 345 567 987]]

# y:
# [[  1   2]
#  [  3   4]
#  [ 82  63]
#  [ 91  52]
#  [121 345]
#  [567 987]]
# 這是二維數(shù)據(jù),6行1列,表示(6,1) 
[[ 0.08540663]
 [ 1.85038409]
 [-2.41396732]
 [ 1.39196365]
 [-0.35908504]
 [ 0.64526911]]

# 這是一維數(shù)據(jù),6行無列(6,)
[ 0.08540663  1.85038409 -2.41396732  1.39196365 -0.35908504  0.64526911]

上面二維變一維:reshape(-1)
一維變二維:reshape(-1,1)

a = np.array([[ 0.08540663],[ 1.85038409],[-2.41396732],[ 1.39196365],[-0.35908504],[ 0.64526911]]) # a是二維數(shù)據(jù)
b = a.reshape(-1)  # b是一維數(shù)據(jù)
c = b.reshape(-1,1) # c是二維數(shù)據(jù)

print("a的維度:{}\n".format(a.shape))
print("b:{}".format(b))
print("b的維度:{}\n".format(b.shape))
print("c:{}".format(c))
print("c的維度:{}".format(c.shape))

# a的維度:(6, 1)

# b:[ 0.08540663  1.85038409 -2.41396732  1.39196365 -0.35908504  0.64526911]
# b的維度:(6,)

# c:[[ 0.08540663]
#  [ 1.85038409]
#  [-2.41396732]
#  [ 1.39196365]
#  [-0.35908504]
#  [ 0.64526911]]
# c的維度:(6, 1)

dataframe

Pandas有兩個主要的數(shù)據(jù)結(jié)構(gòu),Series和DataFrame,記住大小寫區(qū)分。[3]

Series類似于一維數(shù)組,和Numpy的array接近,由一組數(shù)據(jù)和數(shù)據(jù)標(biāo)簽組成。數(shù)據(jù)標(biāo)簽有索引的作用。

Series是一維的數(shù)據(jù)結(jié)構(gòu),DataFrame是一個表格型的數(shù)據(jù)結(jié)構(gòu),它含有不同的列,每列都是不同的數(shù)據(jù)類型。我們可以把DataFrame看作Series組成的字典,它既有行索引也有列索引。

# 這是二維數(shù)據(jù),6行1列,表示(6,1) 
[[ 0.08540663]
 [ 1.85038409]
 [-2.41396732]
 [ 1.39196365]
 [-0.35908504]
 [ 0.64526911]]

# 這是一維數(shù)據(jù),6行無列(6,)
[ 0.08540663  1.85038409 -2.41396732  1.39196365 -0.35908504  0.64526911]

上面二維變一維:reshape(-1)
一維變二維:reshape(-1,1)

a = np.array([[ 0.08540663],[ 1.85038409],[-2.41396732],[ 1.39196365],[-0.35908504],[ 0.64526911]]) # a是二維數(shù)據(jù)
b = a.reshape(-1)  # b是一維數(shù)據(jù)
c = b.reshape(-1,1) # c是二維數(shù)據(jù)

print("a的維度:{}\n".format(a.shape))
print("b:{}".format(b))
print("b的維度:{}\n".format(b.shape))
print("c:{}".format(c))
print("c的維度:{}".format(c.shape))

# a的維度:(6, 1)

# b:[ 0.08540663  1.85038409 -2.41396732  1.39196365 -0.35908504  0.64526911]
# b的維度:(6,)

# c:[[ 0.08540663]
#  [ 1.85038409]
#  [-2.41396732]
#  [ 1.39196365]
#  [-0.35908504]
#  [ 0.64526911]]
# c的維度:(6, 1)

dataframe轉(zhuǎn)化成array:

df=df.values

array轉(zhuǎn)化成dataframe

import pandas as pd
df = pd.DataFrame(df)
import numpy as np
import pandas as pd
a = np.array([[1,2],[1,2]])
b = pd.DataFrame(a)
c = b.values

print(type(a))
print(type(b))
print(type(c))

# out:
# <class 'numpy.ndarray'>
# <class 'pandas.core.frame.DataFrame'>
# <class 'numpy.ndarray'>

參考文章


  1. NumPy中的ndarray與Pandas的Series和DataFrame之間的區(qū)別與轉(zhuǎn)換 ?

  2. python基礎(chǔ)之numpy.reshape詳解 ?

  3. numpy 和 pandas 的區(qū)別詳解 ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容