0

I am trying to read 17 files in a loop where I want to skip 1 row for few files but not for others.

Using the code as given below

import os
import pandas as pd
import glob
path=os.getcwd()
files=os.listdie(os.curdir)
files_xls=[f for f in files if f[-3]=='xls']
filenames=glob.glob(path + "/.xls")
for f in files_xls:
     data=pd.read_excel(f, (skiprows==1 if data.iloc[0]==range(1, 68,1)])

I know the code is incomplete and wrong in last line but my first goal is to able to write the code for skiping rows for few not all files.

Please help me write the code to skip the row.

For eg few sample data looks like below

1 2 3

Name Age Sex

one 23 M

two 32 F

three 46 M

And some sample look like this

Name Age Sex

four 67 M

five 78 F

six 89 M

and Final output should look like this

Name Age Sex

one 23 M

two 32 F

three 46 M

four 67 M

five 78 F

six 89 M

P.S. I am new to programming.

Thanks in advance

2
  • Welcome to StackOverflow. Please provide a sample input with output. Commented Jan 25, 2021 at 8:27
  • @KrishnaChaurasia Edited the question. New to stackoverflow cannot upload picture. Any help would be more than appreciated. Commented Jan 25, 2021 at 9:00

1 Answer 1

0

In general you can do something like this:

data=pd.read_excel(f, skiprows==1 if condition else 0)

But you can't use the data in the file before you read it. The condition can't be function of data (at least you don't read it before in some another way)

Note that in your loop you override the variable data in each call to pd.read_excel. To read files in a loop to DataFrame see here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.