Home » excel » python – How to read merged cells from .xls file using pandas

python – How to read merged cells from .xls file using pandas

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have an excel sheet I’m trying to read in python using pandas. It has some vertically merged cells. the problem is **when reading merged cells, Nan is displayed for them after the first cell. **

Here i am reading the first column “Mr. rattandeepanjea”
enter image description here

The output comes like this with Nan for merged cells, note timeslot 11:20-12:45
enter image description here

My code is:

 import pandas as pd
 xls = pd.ExcelFile("third.xls") #open the file
 sheetX = xls.parse(0)   #move to sheet number 1
 colAneja = sheetX["MR. RATTAN DEEP ANEJA"]
 counter = 9  #the counter is just for showing timeslots out of a list
 for lecture in colAneja:
    print(lisTime[counter%9],": ",lecture)
    counter+=1

`
I would expect the same values for all merged cells, like in this case:

10:55-11:50 - DS LAB(G2) MCA-1(RDA)
11:50-12:45 - DS LAB(G2) MCA-1(RDA)
How to&Answers:

simple

df = df.fillna(method='ffill')