Numpy pandas学习

def test3():#numpy的nan值替换为列平均值
    t1=np.arange(12).reshape((3,4)).astype("float")
    t1[1,2:]=np.nan
    print("t1:")
    print(t1)
    for i in range(t1.shape[1]):#遍历列数
        temp_col=t1[:,i]#取一列
        temp_col_not_nan=temp_col[temp_col==temp_col]#当前一列不为nan,使用布尔索引
        print(temp_col==temp_col)
        print("temp_col_not_nan")
        print(temp_col_not_nan)
        temp_col[np.isnan(temp_col)]=temp_col_not_nan.mean()#将均值赋值给nan
        print("temp_col")
        print(temp_col)
        print("*"*100)

def test4():#数组的拼接
    t1=np.arange(12).reshape((3,4))
    t2=np.arange(12,24).reshape((3,4))
    print("t1")
    print(t1)
    print("t2")
    print(t2)
    print(np.vstack((t1,t2)))#竖直拼接
    print(np.hstack((t1,t2)))#水平拼接
    print("*"*100)
    t1[[1,2],:]=t1[[2,1],:]#行交换
    print(t1)
    t1[:,[0,2]]=t1[:,[2,0]]#列交换
    print(t1)
    #获取最大值最小值的位置
    np.argmax(t1,axis=0)
    np.argmin(t1,axis=1)
    #创建全为0的数组
    np.zeros((3,4))
    #全为1的数组
    np.ones((3,4))
    #创建对角线为1的正方形数组
    np.eye(3)
    # t1=t2.copy() 复制,相互不影响

def test5():#np的随机函数
    np.random.rand(2,3)#产生2行3列的均匀分布的随机数组
    np.random.randn(2,3)#标准正态分布数组
    np.random.randint(10,20,(3,4))#范围[10,20),三行四列的随机整数数组
    np.random.uniform(10,20,(3,4))#产生均匀分布数组
    np.random.seed(10)#设置随机种子


def test1_pandas():#pandas基础
    t = pd.Series([1, 2, 3, 4, 5, 6, 7])  # Series 一维,带标签数组
    print(t)
    t2 = pd.Series([1, 2, 3, 4, 5], index=list("abcde"))  # 指定标签
    print(t2)
    temp_dict = {"name": "xiaohong", "age": 30, "tel": 10086}
    t3 = pd.Series(temp_dict)  # 通过字典创建
    print(t3)
    t3.index  # 索引
    t3.values  #

    # DataFrame
    t3 = pd.DataFrame(np.arange(12).reshape(3, 4))
    print(t3)
    # DataFrame对象既有行索引,又有列索引
    # 行索引,表明不同行,横向索引,叫index,0轴,axis=0
    # 列索引,表名不同列,纵向索引,叫columns,1轴,axis=1
    # ndim 维度属性
    t1 = pd.DataFrame(np.arange(12).reshape(3, 4), index=list("abc"), columns=list("WXYZ"))  # 指定行列的索引
    d1 = {"name": ["xiaoming", "xioahong"], "age": [10, 20], "tel": [100086, 10085]}
    print(pd.DataFrame(d1))  # 通过字典创建
    d2 = [{"name": "xiaohong", "age": 10, "tel": 10086}, {"name": "xiaownag", "age": 10, "tel": 10085}]
    pd.DataFrame(d2)
    t1.head(3)  # 显示头几行
    t1.tail(3)  # 显示末尾几行
    t1.info()  # 展示df的概览
    t1.describe()  # 快速进行统计:count,mean,std,min
    pass

def test_pandas2():#pandas索引
    # pandas索引
    # loc通过标签获取
    t4 = pd.DataFrame(np.arange(12).reshape(3, 4), index=list("abc"), columns=list("WXYZ"))
    print(t4)
    print(t4.loc["a", "Z"])
    print(t4.loc["a", :])
    print(t4.loc[:, "Y"])
    print(t4.loc[["a", "c"], :])
    print(t4.loc[["a", "b"], ["W", "Z"]])
    # iloc通过位置获取
    print(t4.iloc[1, :])
    # 布尔索引
    print(t4[t4["W"] > 2])  # &,|
    # 缺失值处理
    t4.iloc[1] = np.nan
    t4.iloc[1, 2] = 2.0
    print(t4)
    print(pd.isnull(t4))  # 是否为空
    print(pd.notnull(t4))  # 是否不为空
    # 删除
    print(t4[pd.notnull(t4.iloc[:, 0])])  # 把nan所在的行去掉
    t4.dropna(axis=0, how="all", inplace=False)  # 删除nan所在行,all全为nan则删掉,any只要有一个就删掉,inplace是否对源数据修改
    # 填充
    t4.fillna(t4.mean())  # 填充均值
    pass
原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15568285.html