简体   繁体   English

使用 skiprows 导入 Excel 文件

[英]Import Excel file using skiprows

What's the best way to import an excel file?导入 excel 文件的最佳方法是什么? I want ignore all the rows until the date row, 8. Whatever combination of skiprows or headers, I can't get row 8 as the header.我想忽略所有行,直到日期行 8。无论跳过或标题的组合如何,我都无法将第 8 行作为 header。

df = pd.read_excel('sheet.xlsm', skiprows=8, header =1)
df.head(10)
0 col1  col2   Unnamed: 2        Unnamed: 3
1 NaN   comm    comm1            com2
...
8 NaN   NaN     2010-01-01     2010-02-02

I then tried the below, which sort of worked (I ignore the data values for simplicity):然后我尝试了以下方法,哪种方法有效(为简单起见,我忽略了数据值):

df.columns = df.iloc[8].values
  NaN    NaN  2010-01-01     2010-02-02
0
1

But when I try to rename the first two columns that are called NaN, I can't:但是当我尝试重命名名为 NaN 的前两列时,我不能:

df.rename(columns={df.columns[0] :"col1", df.columns[1]: "col2"})
  
   col2    col2      2010-01-01     2010-02-02
0
1

You can do this by:-您可以通过以下方式执行此操作:-

Firstly:-首先:-

df=pd.read_excel('sheet.xlsm')

Now extract the columns value:-现在提取列值:-

col=df.columns

Now finally:-现在终于:-

df = pd.read_excel('sheet.xlsm', skiprows=8)
df.columns=col

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM