Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

https://geek-docs.com/pandas/pandas-read-write/pandas-to-read-and-write-excel.html

read_excel可以把excel读成dataframes
to_excel把dataframes写成excel

读excel

import pandas as pd
df = pd.read_excel("copy.xls", 1)

1是指定第二个sheet,默认是读取第一个sheet
会自动识别列的内容类型,比如是int还是str,不过也可以通过传参dtype={'col_name':str}来指定哪些列读取的数据的类型

写excel

写多个sheet

frame = pd.DataFrame(
    np.random.random((4, 4)),
    index=["exp1", "exp2", "exp3", "exp4"],
    columns=["jan2015", "Fab2015", "Mar2015", "Apr2005"],
)
with pd.ExcelWriter("test.xlsx") as ew:
    frame.to_excel(ew, sheet_name="1")
    frame.to_excel(ew, sheet_name="2")

写入一个sheet

frame.to_excel("test.xlsx", sheet_name="1")

操作df

获取列名

df.columns

获取拷贝

获取行

df[1:2] # 行1
df[1:3] # 行1和行2

获取某行某列

df[1:2]['年龄'] # 行1的年龄列

获取引用

https://blog.csdn.net/qq_18351157/article/details/104838924
某行某列

df.at[1,'年龄'] # 行1的年龄列

合并df

https://blog.csdn.net/qq_41853758/article/details/83280104
推荐使用merge和concat:支持内外连接左右连接

评论