02python 基础笔记 numpy / pandas
import numpy as np
import random
#https://www.bilibili.com/video/BV1rJ411g7Mz?p=20
#定义numpy数组 使用np.array和np.arange
t1 = np.arange(12)
#t1 = np.array([1,2,3,4,5]) #同上
print("t1:",t1,'\n',t1.shape)
#定义二维数组 shape[0]行数 shape[1]列数
t2 = np.array([[1,2,3],[4,5,6]])
print("t2:",t2,'\n',t2.shape)
#定义三维数组
t3 = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
print("t3:",t3,'\n',t3.shape)
#reshape变成二维数组
t4 = t1.reshape(3,-1)
print("t4:",t4,'\n',t4.shape)
#reshape变成一维数组
# t4 = t4.reshape(-1) #同下flatten
t4 = t4.flatten()
print("t4:",t4,'\n',t4.shape,'\n',t4.dtype)
#astype
t5 = np.array([1,0,1,1,0,0,1,0,1,0,1],dtype=bool)
print("t5:",t5,'\n',t5.shape,'\n',t5.dtype)
t6 = t5.astype("int8")
print("t6:",t6,'\n',t6.shape,'\n',t6.dtype)
t7 = np.array([random.random() for i in range(10)])
print("t7:",t7,'\n',t7.shape,'\n',t7.dtype)
#round取小数点位数
t8 = np.round(t7,2)
print("t8:",t8,'\n',t8.shape,'\n',t8.dtype)
# 对于二维数组 axis=0行,axis=1列
# 对于三维数组 axis=0三维数组中二维数组的个数,axis=1行,axis=2列
file_path = "../BJXS1_df_test_with_needsnow.csv"
f1 = np.loadtxt(file_path,delimiter=",",dtype="str")
print(f1)
#丢掉第一行字段名
f1 = f1[1:]
print(f1)
#取一行
print(f1[1])
print(f1[1,:])
#取多行
print(f1[[1,3,5]])#取1,3,5行的全部字段
print(f1[[1,3,5],:])#取1,3,5行的全部字段
print(f1[[1,3,5],0:3])#取1,3,5行的1,2,3列
#取列
print(f1[:,0])#第一列
print(f1[:,2:])#取除第一列的所有列
print(f1[:,[0,3,5]])#取不连续的多列
#取任意的行列
print(f1[0:10,0:3])#连续
print("----------------------------------------")
print(f1[0:5,[0,1]])#不连续
#where
q = np.arange(24).reshape((4,6))
q = np.where(q<10,0,10)
print(q)
q = q.astype(float)
q[3,3] = np.nan
print(q)
#数组拼接
q1 = np.arange(20).astype(int).reshape(2,10)
q2 = np.arange(20).astype(int).reshape(2,10)
#竖直拼接
q3=np.vstack((q1,q2))
#水平拼接
q4=np.hstack((q1,q2))
print("竖直拼接:","\n",q3)
print("水平拼接:","\n",q4)
#数组的行列交换
q5 = np.arange(12,24).reshape(3,4)
print(q5)
q5[[1,2],:] = q5[[2,1],:]#交换行
print(q5)
q5[:,[0,2]] = q5[:,[2,0]]#交换列
print(q5)
from matplotlib import pyplot as plt
import matplotlib
matplotlib.rc("font",family='MicroSoft YaHei',weight="bold")
#视屏图二
y = f1[:,3]
print(y)
y1 = f1[:,2]
print(y1)
x = f1[:,0]
plt.figure(figsize=(30,10),dpi=80)
plt.plot(x,y,label='111',color="orange",linestyle=":")
plt.plot(x,y1,label='222',color="red",linestyle="--")
_xtick_labels = ["{}日".format(i) for i in x]
# plt.xticks(x,_xtick_labels)
plt.xticks(list(x)[::3],_xtick_labels[::3],rotation=300)
# plt.yticks(list(y)[::5])
# plt.yticks(len(y))
plt.grid(alpha=0.5)#透明度
plt.legend() #显示图例
plt.show()
#对角矩阵
s = np.eye(10)
print(s)
#每一行列的最大最小值
print(np.argmax(s,axis=0))
s1 = np.random.randint(10,20,(4,5))#生成整形随机数组
print(np.argmin(s1,axis=0))
t1: [ 0 1 2 3 4 5 6 7 8 9 10 11]
(12,)
t2: [[1 2 3]
[4 5 6]]
(2, 3)
t3: [[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
(2, 2, 3)
t4: [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
(3, 4)
t4: [ 0 1 2 3 4 5 6 7 8 9 10 11]
(12,)
int32
t5: [ True False True True False False True False True False True]
(11,)
bool
t6: [1 0 1 1 0 0 1 0 1 0 1]
(11,)
int8
t7: [0.26206672 0.54238283 0.20224875 0.46643833 0.40129415 0.30012872
0.74340253 0.0067845 0.11311805 0.34403928]
(10,)
float64
t8: [0.26 0.54 0.2 0.47 0.4 0.3 0.74 0.01 0.11 0.34]
(10,)
float64
[['areaDate' 'AAAA_max' 'AAGA_max' ... 'AAFB_mean' 'AAHA1D_sum'
'needsnow']
['2019-01-03' '-11' '793' ... '5' '66' '0']
['2019-01-04' '-17' '793' ... '5' '0' '0']
...
['2019-06-11' '22' '792' ... '1' '0' '-1']
['2019-06-12' '18' '792' ... '1' '0' '-1']
['2019-06-13' '11' '789' ... '1' '0' '-1']]
[['2019-01-03' '-11' '793' ... '5' '66' '0']
['2019-01-04' '-17' '793' ... '5' '0' '0']
['2019-01-05' '-7' '793' ... '1' '0' '0']
...
['2019-06-11' '22' '792' ... '1' '0' '-1']
['2019-06-12' '18' '792' ... '1' '0' '-1']
['2019-06-13' '11' '789' ... '1' '0' '-1']]
['2019-01-04' '-17' '793' '59' '359' '14' '-22' '790' '35' '0' '0' '-19'
'791' '47' '313' '5' '0' '0']
['2019-01-04' '-17' '793' '59' '359' '14' '-22' '790' '35' '0' '0' '-19'
'791' '47' '313' '5' '0' '0']
[['2019-01-04' '-17' '793' '59' '359' '14' '-22' '790' '35' '0' '0' '-19'
'791' '47' '313' '5' '0' '0']
['2019-01-06' '-10' '792' '80' '360' '9' '-17' '788' '30' '0' '0' '-12'
'790' '47' '282' '3' '0' '0']
['2019-01-08' '-13' '795' '70' '359' '11' '-23' '790' '13' '0' '0' '-18'
'793' '48' '263' '3' '0' '0']]
[['2019-01-04' '-17' '793' '59' '359' '14' '-22' '790' '35' '0' '0' '-19'
'791' '47' '313' '5' '0' '0']
['2019-01-06' '-10' '792' '80' '360' '9' '-17' '788' '30' '0' '0' '-12'
'790' '47' '282' '3' '0' '0']
['2019-01-08' '-13' '795' '70' '359' '11' '-23' '790' '13' '0' '0' '-18'
'793' '48' '263' '3' '0' '0']]
[['2019-01-04' '-17' '793']
['2019-01-06' '-10' '792']
['2019-01-08' '-13' '795']]
['2019-01-03' '2019-01-04' '2019-01-05' '2019-01-06' '2019-01-07'
'2019-01-08' '2019-01-09' '2019-01-10' '2019-01-11' '2019-01-12'
'2019-01-13' '2019-01-14' '2019-01-15' '2019-01-16' '2019-01-17'
'2019-01-18' '2019-01-19' '2019-01-20' '2019-01-21' '2019-01-22'
'2019-01-23' '2019-01-24' '2019-01-25' '2019-01-26' '2019-01-27'
'2019-01-28' '2019-01-29' '2019-01-30' '2019-01-31' '2019-02-01'
'2019-02-02' '2019-02-03' '2019-02-04' '2019-02-05' '2019-02-06'
'2019-02-13' '2019-02-14' '2019-02-15' '2019-02-16' '2019-02-17'
'2019-02-18' '2019-02-19' '2019-02-20' '2019-02-21' '2019-02-22'
'2019-02-23' '2019-02-24' '2019-02-25' '2019-02-26' '2019-02-27'
'2019-02-28' '2019-03-01' '2019-03-02' '2019-03-04' '2019-03-05'
'2019-03-06' '2019-03-07' '2019-03-08' '2019-03-09' '2019-03-10'
'2019-03-11' '2019-03-12' '2019-03-13' '2019-03-14' '2019-03-15'
'2019-03-16' '2019-03-17' '2019-03-18' '2019-03-19' '2019-03-20'
'2019-03-21' '2019-03-22' '2019-03-23' '2019-03-24' '2019-03-25'
'2019-03-26' '2019-03-27' '2019-03-28' '2019-03-29' '2019-03-30'
'2019-03-31' '2019-04-01' '2019-04-02' '2019-04-03' '2019-04-04'
'2019-04-05' '2019-04-06' '2019-04-07' '2019-04-08' '2019-04-09'
'2019-04-10' '2019-04-11' '2019-04-12' '2019-04-15' '2019-04-16'
'2019-04-17' '2019-04-18' '2019-04-19' '2019-04-20' '2019-04-21'
'2019-04-22' '2019-04-28' '2019-04-29' '2019-04-30' '2019-05-01'
'2019-05-02' '2019-05-03' '2019-05-05' '2019-05-06' '2019-05-07'
'2019-05-08' '2019-05-09' '2019-05-10' '2019-05-11' '2019-05-13'
'2019-05-14' '2019-05-15' '2019-05-16' '2019-05-17' '2019-05-21'
'2019-05-22' '2019-05-23' '2019-05-24' '2019-05-25' '2019-05-26'
'2019-05-27' '2019-05-28' '2019-05-29' '2019-05-30' '2019-05-31'
'2019-06-01' '2019-06-05' '2019-06-06' '2019-06-07' '2019-06-08'
'2019-06-09' '2019-06-10' '2019-06-11' '2019-06-12' '2019-06-13']
[['793' '78' '360' ... '5' '66' '0']
['793' '59' '359' ... '5' '0' '0']
['793' '61' '360' ... '1' '0' '0']
...
['792' '67' '360' ... '1' '0' '-1']
['792' '71' '248' ... '1' '0' '-1']
['789' '84' '129' ... '1' '0' '-1']]
[['2019-01-03' '78' '13']
['2019-01-04' '59' '14']
['2019-01-05' '61' '9']
['2019-01-06' '80' '9']
['2019-01-07' '73' '10']
['2019-01-08' '70' '11']
['2019-01-09' '73' '7']
['2019-01-10' '76' '10']
['2019-01-11' '77' '6']
['2019-01-12' '67' '10']
['2019-01-13' '66' '10']
['2019-01-14' '73' '13']
['2019-01-15' '72' '13']
['2019-01-16' '37' '11']
['2019-01-17' '48' '9']
['2019-01-18' '65' '11']
['2019-01-19' '72' '9']
['2019-01-20' '70' '13']
['2019-01-21' '59' '14']
['2019-01-22' '63' '12']
['2019-01-23' '62' '6']
['2019-01-24' '68' '9']
['2019-01-25' '53' '12']
['2019-01-26' '47' '8']
['2019-01-27' '59' '14']
['2019-01-28' '51' '14']
['2019-01-29' '69' '10']
['2019-01-30' '64' '11']
['2019-01-31' '33' '12']
['2019-02-01' '40' '11']
['2019-02-02' '47' '10']
['2019-02-03' '71' '15']
['2019-02-04' '81' '9']
['2019-02-05' '72' '9']
['2019-02-06' '76' '7']
['2019-02-13' '67' '3']
['2019-02-14' '81' '12']
['2019-02-15' '75' '13']
['2019-02-16' '65' '9']
['2019-02-17' '66' '8']
['2019-02-18' '84' '6']
['2019-02-19' '83' '9']
['2019-02-20' '58' '8']
['2019-02-21' '50' '4']
['2019-02-22' '55' '4']
['2019-02-23' '59' '5']
['2019-02-24' '51' '7']
['2019-02-25' '63' '4']
['2019-02-26' '88' '4']
['2019-02-27' '87' '7']
['2019-02-28' '61' '6']
['2019-03-01' '52' '6']
['2019-03-02' '54' '7']
['2019-03-04' '51' '5']
['2019-03-05' '62' '10']
['2019-03-06' '77' '12']
['2019-03-07' '45' '5']
['2019-03-08' '36' '7']
['2019-03-09' '91' '5']
['2019-03-10' '90' '10']
['2019-03-11' '85' '14']
['2019-03-12' '51' '13']
['2019-03-13' '67' '12']
['2019-03-14' '78' '13']
['2019-03-15' '65' '13']
['2019-03-16' '58' '11']
['2019-03-17' '58' '8']
['2019-03-18' '37' '7']
['2019-03-19' '48' '7']
['2019-03-20' '94' '9']
['2019-03-21' '85' '13']
['2019-03-22' '83' '12']
['2019-03-23' '70' '11']
['2019-03-24' '40' '9']
['2019-03-25' '32' '10']
['2019-03-26' '28' '8']
['2019-03-27' '79' '10']
['2019-03-28' '87' '5']
['2019-03-29' '88' '14']
['2019-03-30' '82' '13']
['2019-03-31' '71' '12']
['2019-04-01' '60' '6']
['2019-04-02' '37' '7']
['2019-04-03' '35' '8']
['2019-04-04' '42' '14']
['2019-04-05' '46' '8']
['2019-04-06' '50' '8']
['2019-04-07' '64' '8']
['2019-04-08' '79' '6']
['2019-04-09' '90' '8']
['2019-04-10' '89' '9']
['2019-04-11' '61' '6']
['2019-04-12' '53' '7']
['2019-04-15' '37' '7']
['2019-04-16' '55' '7']
['2019-04-17' '61' '16']
['2019-04-18' '65' '9']
['2019-04-19' '94' '8']
['2019-04-20' '97' '9']
['2019-04-21' '60' '8']
['2019-04-22' '59' '9']
['2019-04-28' '93' '7']
['2019-04-29' '93' '12']
['2019-04-30' '85' '17']
['2019-05-01' '28' '9']
['2019-05-02' '24' '7']
['2019-05-03' '31' '4']
['2019-05-05' '52' '12']
['2019-05-06' '35' '8']
['2019-05-07' '34' '8']
['2019-05-08' '39' '9']
['2019-05-09' '39' '9']
['2019-05-10' '38' '6']
['2019-05-11' '71' '1']
['2019-05-13' '26' '7']
['2019-05-14' '39' '7']
['2019-05-15' '79' '9']
['2019-05-16' '55' '6']
['2019-05-17' '46' '5']
['2019-05-21' '43' '12']
['2019-05-22' '32' '11']
['2019-05-23' '24' '8']
['2019-05-24' '58' '5']
['2019-05-25' '92' '7']
['2019-05-26' '99' '15']
['2019-05-27' '89' '15']
['2019-05-28' '38' '10']
['2019-05-29' '46' '6']
['2019-05-30' '50' '9']
['2019-05-31' '36' '10']
['2019-06-01' '41' '6']
['2019-06-05' '49' '5']
['2019-06-06' '94' '5']
['2019-06-07' '93' '9']
['2019-06-08' '79' '11']
['2019-06-09' '91' '11']
['2019-06-10' '69' '8']
['2019-06-11' '67' '5']
['2019-06-12' '71' '5']
['2019-06-13' '84' '1']]
[['2019-01-03' '-11' '793']
['2019-01-04' '-17' '793']
['2019-01-05' '-7' '793']
['2019-01-06' '-10' '792']
['2019-01-07' '-16' '792']
['2019-01-08' '-13' '795']
['2019-01-09' '-8' '795']
['2019-01-10' '-8' '790']
['2019-01-11' '-6' '790']
['2019-01-12' '-8' '791']]
----------------------------------------
[['2019-01-03' '-11']
['2019-01-04' '-17']
['2019-01-05' '-7']
['2019-01-06' '-10']
['2019-01-07' '-16']]
[[ 0 0 0 0 0 0]
[ 0 0 0 0 10 10]
[10 10 10 10 10 10]
[10 10 10 10 10 10]]
[[ 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 10. 10.]
[10. 10. 10. 10. 10. 10.]
[10. 10. 10. nan 10. 10.]]
竖直拼接:
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]]
水平拼接:
[[ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19 10 11 12 13 14 15 16 17 18 19]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[12 13 14 15]
[20 21 22 23]
[16 17 18 19]]
[[14 13 12 15]
[22 21 20 23]
[18 17 16 19]]
['78' '59' '61' '80' '73' '70' '73' '76' '77' '67' '66' '73' '72' '37'
'48' '65' '72' '70' '59' '63' '62' '68' '53' '47' '59' '51' '69' '64'
'33' '40' '47' '71' '81' '72' '76' '67' '81' '75' '65' '66' '84' '83'
'58' '50' '55' '59' '51' '63' '88' '87' '61' '52' '54' '51' '62' '77'
'45' '36' '91' '90' '85' '51' '67' '78' '65' '58' '58' '37' '48' '94'
'85' '83' '70' '40' '32' '28' '79' '87' '88' '82' '71' '60' '37' '35'
'42' '46' '50' '64' '79' '90' '89' '61' '53' '37' '55' '61' '65' '94'
'97' '60' '59' '93' '93' '85' '28' '24' '31' '52' '35' '34' '39' '39'
'38' '71' '26' '39' '79' '55' '46' '43' '32' '24' '58' '92' '99' '89'
'38' '46' '50' '36' '41' '49' '94' '93' '79' '91' '69' '67' '71' '84']
['793' '793' '793' '792' '792' '795' '795' '790' '790' '791' '792' '791'
'793' '790' '790' '791' '791' '793' '791' '789' '791' '794' '799' '799'
'790' '791' '790' '793' '790' '786' '783' '786' '785' '782' '783' '793'
'792' '790' '792' '792' '789' '788' '796' '796' '795' '795' '792' '793'
'792' '790' '791' '792' '791' '788' '791' '795' '795' '792' '789' '788'
'785' '786' '791' '791' '791' '793' '794' '789' '786' '783' '790' '791'
'794' '789' '789' '789' '790' '789' '785' '790' '792' '793' '797' '797'
'792' '788' '788' '787' '789' '790' '791' '790' '788' '791' '788' '786'
'792' '792' '791' '792' '790' '792' '790' '792' '795' '794' '794' '799'
'799' '795' '790' '790' '789' '790' '786' '789' '789' '789' '788' '788'
'788' '792' '790' '790' '790' '796' '796' '794' '791' '792' '790' '793'
'792' '790' '789' '791' '792' '792' '792' '789']
**
def fill_ndarrary(s2):
for i in range(s2.shape[1]):
temp_col = s2[:,i]
nan_num = np.count_nonzero(temp_col!=temp_col)
if nan_num != 0:
temp_not_nan_col = temp_col[temp_col==temp_col]
#选中当前为nan的位置,赋值为不为nan的均值填充
temp_col[np.isnan(temp_col)] = temp_not_nan_col.mean()
return s2
if __name__ == '__main__':
s2 = np.arange(12).reshape((3, 4)).astype("float")
s2[1, 2:] = np.nan
print(s2)
s2 = fill_ndarrary(s2)
print(s2)**
[[ 0. 1. 2. 3.]
[ 4. 5. nan nan]
[ 8. 9. 10. 11.]]
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]]