简体   繁体   中英

Import Excel file using skiprows

What's the best way to import an excel file? 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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