美文网首页
2020-07-14

2020-07-14

作者: 小糖豆儿er | 来源:发表于2020-07-14 23:47 被阅读0次

    今日目标--80%
    用python实现excel转置

    1. 要读取excel的内容以及行列数
      形成数组
    2. 从形成的数组中读取内容
      3.建立一个行列数转置的空数组
      4.将读取的内容写到新的位置中

    问题:

    1. 读取行列数
    import pandas as pd
    df=pd.read_excel('/Users/tang/Desktop/AB.xlsx')
    height,width=df.shape
    print(height,width,type(df))
    print(df) #第一行会被识别为表头 要记得+1
    

    运行结果

    14 7 <class 'pandas.core.frame.DataFrame'> #height+1=行数 width=列数
       2011   7  6    82-85 7.1  6.1  82-85.1
    0   2011   7  6    90-97   7    6    90-97
    1   2011   7  6  104-108   7    6  104-108
    2   2011  Z2  6    21-25  Z2    6    21-25
    3   2011  Z2  6    26-28  Z2    6    26-28
    4   2011  Z2  6    29-32  Z2    6    29-32
    5   2011  Z2  6    33-36  Z2    6    33-36
    6   2011  Z2  6    37-40  Z2    6    37-40
    7   2011  Z2  6    41-45  Z2    6    41-45
    8   2011  Z2  6    46-49  Z2    6    46-49
    9   2011  Z2  6    50-55  Z2    6    50-55
    10  2011  Z2  6    56-60  Z2    6    56-60
    11  2011  Z2  6    61-65  Z2    6    61-65
    12  2011  Z2  6    66-70  Z2    6    66-70
    13  2011  Z2  6    71-76  Z2    6    71-76
    

    2、 新建一个转置数组,行数=width 列数=height+1

    import numpy as np
    x=np.zeros((width, height+1))
    print (x.shape)
    print(x)
    

    运行结果

    (7, 15)
    [[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
     [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
    

    3、赋值
    还没看明白。。明天再说吧

    相关文章

      网友评论

          本文标题:2020-07-14

          本文链接:https://www.haomeiwen.com/subject/rndqhktx.html