• pandas创建及读取excel文件


    创建文件

    • 将DataFrame数据写入excel文件。
    • 没有限制时,会自动添加index。
    • 用字典创建时的key则自动为column。
    • 如果想去掉自动生成的index,可以用set_index指定,但不会改变原df。如果想要改变,使用inplace参数。
    import pandas as pd
    file = r"D:\xlsx\test1.xlsx"
    df1 = pd.DataFrame({"ID": ["a", "b", "c"], "name": ["Lucy", "Jim", "Lily"], "score": [70, 80, 90]})
    print(df1)
    df1.to_excel(file)
    
    • 1
    • 2
    • 3
    • 4
    • 5
      ID  name  score
    0  a  Lucy     70
    1  b   Jim     80
    2  c  Lily     90
    
    • 1
    • 2
    • 3
    • 4

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ialhr6fy-1661600614670)(attachment:image.png)]

    df1 = df1.set_index("ID")   #注意原df1不改变
    print(df1)
    
    • 1
    • 2
        name  score
    ID             
    a   Lucy     70
    b    Jim     80
    c   Lily     90
    
    • 1
    • 2
    • 3
    • 4
    • 5

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RaHVqYbh-1661600614673)(attachment:image.png)]

    df1 = pd.DataFrame({"ID": ["a", "b", "c"], "name": ["Lucy", "Jim", "Lily"], "score": [70, 80, 90]})
    df1.set_index("ID", inplace=True)   #注意原df1改变
    print(df1)
    
    • 1
    • 2
    • 3
        name  score
    ID             
    a   Lucy     70
    b    Jim     80
    c   Lily     90
    
    • 1
    • 2
    • 3
    • 4
    • 5

    读取文件

    • 当从excel读取文件时,也会自动在生成的数据上加index。
    import pandas as pd
    file_src = r"D:\xlsx\test1.xlsx"
    file_dst = r"D:\xlsx\test2.xlsx"
    df1 = pd.read_excel(file_src)       #默认读出的数据会自动添加index, 会将首行数据作为column
    print(df1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
      ID  name  score
    0  a  Lucy     70
    1  b   Jim     80
    2  c  Lily     90
    
    • 1
    • 2
    • 3
    • 4

    查看行列及前后几行

    df1.head(2)
    
    • 1
    IDnamescore
    0aLucy70
    1bJim80
    df1.tail(2)
    
    • 1
    IDnamescore
    1bJim80
    2cLily90
    df1.shape
    
    • 1
    (3, 3)
    
    • 1

    数据header不是excel的第一行

    • 使用header指定第几行,注意excel的第一行对应的header为0

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BttICNzc-1661600614674)(attachment:image.png)]

    df1 = pd.read_excel(file_src,header=1) 
    df1.columns
    
    • 1
    • 2
    Index(['ID', 'name', 'score'], dtype='object')
    
    • 1

    数据无header

    • 在读取时header指定为None,使用columns指定header
    • 在读取时,使用names指定

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H4CSL6m2-1661600614675)(attachment:image.png)]

    # 使用header和columns配合
    df1 = pd.read_excel(file_src, header=None) 
    df1.columns = ["ID", "name", "score"]
    df1.columns
    
    • 1
    • 2
    • 3
    • 4
    Index(['ID', 'name', 'score'], dtype='object')
    
    • 1
    # 使用names指定
    df1 = pd.read_excel(file_src, names=["ID", "name", "score"]) 
    df1.columns
    
    • 1
    • 2
    • 3
    Index(['ID', 'name', 'score'], dtype='object')
    
    • 1

    读入数据时将index删除掉

    • 使用index_col参数设置读入时的index
    file_src =  r"D:\xlsx\test2.xlsx"
    df1 = pd.read_excel(file_src)
    df1
    
    • 1
    • 2
    • 3
    IDnamescore
    0aLucy70
    1bJim80
    2cLily90
    file_src =  r"D:\xlsx\test2.xlsx"
    file_dst = r"D:\xlsx\test3.xlsx"
    df1 = pd.read_excel(file_src, index_col="ID")
    print(df1)
    df1.to_excel(file_dst)
    
    • 1
    • 2
    • 3
    • 4
    • 5
        name  score
    ID             
    a   Lucy     70
    b    Jim     80
    c   Lily     90
    
    • 1
    • 2
    • 3
    • 4
    • 5

    读取指定的列

    • 使用usecols指定,可以为数字或者字母,列表或者切片。

    指定读取的行数

    • nrows=数字
    file_src =  r"D:\xlsx\test2.xlsx"
    df1 = pd.read_excel(file_src, nrows=2, index_col="ID")
    print(df1)
    
    • 1
    • 2
    • 3
        name  score
    ID             
    a   Lucy     70
    b    Jim     80
    
    • 1
    • 2
    • 3
    • 4

    指定跳过的行

    • skiprows=列表
    file_src =  r"D:\xlsx\test2.xlsx"
    df1 = pd.read_excel(file_src, skiprows=[1,3], index_col="ID")
    print(df1)
    
    • 1
    • 2
    • 3
       name  score
    ID            
    b   Jim     80
    
    • 1
    • 2
    • 3

    参考

    详细可参考 https://editor.csdn.net/md/?articleId=126057819

  • 相关阅读:
    滚雪球学Java(45):探秘Java Runtime类:深入了解JVM运行时环境
    Java从入门到精通-类和对象(三)
    VR全景图比平面图多了哪些优势,VR全景可以用在哪些领域
    Android 唤醒应用程序,APP间传递数据
    uniapp问题归类
    DispatcherServlet初始化之遍历HandlerMethod
    基于Java+freemarker实现动态赋值以及生成Word文档
    怎么把pdf转换成ppt呢?轻松简单
    微信小程序:EventChannel实现页面间事件通信通道
    SpringBoot+MyBatisPlus+AOP实现多数据源切换
  • 原文地址:https://blog.csdn.net/weixin_48668114/article/details/126561674