python(10)-pandas(2)-EXCEL操作-实例源码

EXCEL文件是常用操作。这里是pandas对EXCEL的一般操作

1.创建文件   
2.读取EXCEL文件  返回的类型:DataFrame  pd.read_excel()
  2.1读取默认表单
  2.2读取指定sheet
  2.3读取指定行列的值         data=df.loc[[1,2]].values    ix已弃
3.拼接 横向 纵向              merge() 主要用于多表横向拼接 pd.merge(table_a, table_b, on="序号")
                              concat() 主要用于纵向拼接 pd.concat([table_a, table_b], axis=0)
4.index 索引需要重置         table_c.reset_index(drop=True,inplace=True)
5.['列号']更新               table_c['序号']=range(1,len(table_c)+1)
6.合并后的数据保存为         table_c.to_excel("c.xlsx",index=False)
7.提取C表的某行某列          part_c=table_c[:2]

1.创建文件    
创建EXCEL  pd.ExcelWriter()
    
     data = np.arange(1, 21).reshape((10, 2))  #(1, 21)序列 (10, 2) 行 列
     data_df = pd.DataFrame(data)
     data_df.columns = ['序号', '标识名']
     data_df.index = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
     writer = pd.ExcelWriter('write.xlsx')
     data_df.to_excel(writer, float_format='%.5f')
     writer.save()

"""
    文件输出:sheet1
    	   序号	 标识名
        1	1	2
        2	3	4
        3	5	6
        4	7	8
        5	9	10
        6	11	12
        7	13	14
        8	15	16
        9	17	18
        10	19	20

"""
 
2.读取EXCEL文件  返回的类型:DataFrame  pd.read_excel()
  2.1读取默认表单
  2.2读取指定sheet
  2.3读取指定行列的值         data=df.loc[[1,2]].values    ix已弃

 #2.1读取 默认读第一个表单 返回的是DataFrame
def fun1():
     name_a="a.xlsx"
     name_b="b.xlsx"
     table_a=pd.read_excel(name_a)
     table_b=pd.read_excel(name_b)
     print(table_a)
     print(table_b)
     print(type(table_b))

"""
         文件输出:sheet1
   序号   标识名
0   0  A_sheet1_0
1   1  A_sheet1_1
  序号     标识名
0   0  B_sheet1_ana0
1   1  B_sheet1_ana1

"""
 #2.2 读取指定sheet
 #2.3 指定位置的值
#def fun2():
    df = pd.read_excel("a.xlsx", sheet_name=["sheet1","sheet3"])             # 通过 表单名 同时指定多个
    print(df)
"""
     OrderedDict([('sheet1',    序号         标识名
    0   0  A_sheet1_0
    1   1  A_sheet1_1
    2   2  A_sheet1_2), ('sheet3',    序号         标识名
    0   0  A_sheet3_0
    1   1  A_sheet3_1)])
"""

    df = pd.read_excel("a.xlsx", sheet_name='sheet3')    # 通过 表单名 同时指定1个 sheet3
    df = pd.read_excel("a.xlsx", sheet_name = 0)         # 通过 表单 索引来指定读取的表单 sheet_name = 0b表示sheet1
   df = pd.read_excel("a.xlsx",sheet_name=["sheet1",2])  # 通过 混合的方式来指定 "sheet_name" +"index"
   df = pd.read_excel("a.xlsx", sheet_name=[1, 2])    # 通过 索引   指定多个(sheet2 sheet3 )
   df = pd.read_excel("write.xlsx", sheet_name="sheet1")
   print("读取前5行数据:\n{0}".format(df.head()))                         #读取前5行
   print("读取index=5 的数据:\n{0}".format(df.loc[5].values))
   print( "读取1,3行的数据:\n{0}",df.loc[[1, 3]].values)                 #指定1 3 两行的值
   print("输出行号列表:\n,", df.columns.values)
   print("输出数值", df.sample(2).values)             # 这个方法类似于head()方法以及df.values方法
   print("输出值\n", df['标识名'].values)

3.拼接 横向 纵向              merge() 主要用于多表横向拼接 pd.merge(table_a, table_b, on="序号")
                              concat() 主要用于纵向拼接 pd.concat([table_a, table_b], axis=0)

 name_a = "a.xlsx"
    name_b = "b.xlsx"
    table_a = pd.read_excel(name_a)
    table_b = pd.read_excel(name_b)
    table_c = pd.concat([table_a, table_b], axis=0)
    """
     序号            标识名
0   0     A_sheet1_0
1   1     A_sheet1_1
2   2     A_sheet1_2
0   0  B_sheet1_ana0
1   1  B_sheet1_ana1
2   2  B_sheet1_ana2
    """
     table_c= pd.merge(table_a, table_b, on="序号")
"""
输出
序号       标识名_x          标识名_y
0   0  A_sheet1_0  B_sheet1_ana0
1   1  A_sheet1_1  B_sheet1_ana1
2   2  A_sheet1_2  B_sheet1_ana2

"""
4.index 索引需要重置         table_c.reset_index(drop=True,inplace=True)
5.['列号']更新               table_c['序号']=range(1,len(table_c)+1)
6.合并后的数据保存为         table_c.to_excel("c.xlsx",index=False)
7.提取C表的某行某列          part_c=table_c[:2]
 #4.重置索引
     table_c.reset_index(drop=True,inplace=True)
     print(table_c)

    #5.['列号']数字更新
     table_c['序号']=range(1,len(table_c)+1)
     print(table_c)

    #6.合并后的数据保存为result.xlsx
     table_c.to_excel("c.xlsx",index=False)

    #7.提取C表的某行某列 [行,列]{列 0 1 2 }
     part_c=table_c[:2]
     print(part_c)

 

posted @ 2019-12-15 21:42  jasmineTang  阅读(142)  评论(0)    收藏  举报