# Pandas NumPy用法详细解释

NumPy主要用C语言编写, 它是Python的扩展模块。

Pandas建立在numpy数组之上；因此, numpy帮助我们更有效地使用Pandas。

``````# import the "array" for demonstrating array operations
import array
# initializing an array with array values and signed integers
arr = array.array('l', [2, 4, 6, 8, 10, 12])
# print the original array
print ("New created array: ", end="")
for l in range (0, 5):
print (arr[l], end=" ")
print ("\r")``````

``New created array: 2 4 6 8 10``

### 布尔索引

• 使用布尔索引访问DataFrame。
• 将布尔掩码应用于DataFrame。
• 根据列值屏蔽数据。
• 根据索引值屏蔽数据。

``````# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["Smith", "William", "Phill", "Parker"], 'age': ["28", "39", "34", "36"]}
info = pd.DataFrame(dict, index = [True, True, False, True])
print(info)``````

``````name    age
True     Smith    28
True   William   39
False    Phill   34
True    Parker    36``````

``````# importing pandas as pd
import pandas as pd
# dictionary of lists
dict = {'name':["Smith", "William", "Phill", "Parker"], 'age': ["28", "39", "34", "36"]}
info = pd.DataFrame(dict, index = [True, True, False, True])
# accessing a dataframe using .loc[] function
print(info.loc[True])``````

``````name    age
True     Smith    28
True   William    39
True    Parker    36``````

### 重塑数组

``numpy.reshape(a, newshape, order='C')``

• a：定义要整形的数组。
• newshape：定义应与原始形状兼容的新形状。对于整数值, 结果将是该长度的一维数组。一个形状尺寸可以为-1。
• order：这是一个可选参数, 它通过使用索引顺序读取元素, 然后借助索引顺序将元素放置到重新排列的数组中。

``````import numpy as np
arr = np.arange(16)
print("The Original array is: \n", arr)
# shape array with 2 rows and 8 columns
arr = np.arange(16).reshape(2, 8)
print("\nreshapedarray: \n", arr)
# shape array with 2 rows and 8 columns
arr = np.arange(16).reshape(8 , 2)
print("\nreshaped array: \n", arr)``````

``````The Original array is:
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]
reshaped array:
[[ 0  1  2  3  4  5  6  7]
[ 8  9 10 11 12 13 14 15]]

reshaped array:
[[ 0  1]
[ 2  3]
[ 4  5]
[ 6  7]
[ 8  9]
[10 11]
[12 13]
[14 15]]``````