0

I am reading a csv into a dataframe using:

import pandas as pd
df = pd.read_csv('file.csv') 

My csv has 800 rows of data, but my dataframe is reading in 805 rows. The last 5 rows are completely blank. Is there any way to remove these 5 empty rows?

2
  • you can use built-in function dropna() for NaN data Commented Jul 17, 2017 at 19:14
  • you can also try df=pd.read_csv('file.csv', skipfooter=5) Commented Jul 18, 2017 at 14:02

2 Answers 2

2

You can use slice with iloc to remove last 5 rows, if you don't bother reading the data again and just want to work with the current copy:

df = df.iloc[:-5]

Or use head:

df.head(-5)
Sign up to request clarification or add additional context in comments.

Comments

1

You have a few options when using the read_csv() from pandas which is documented here.

I think the easiest one-time argument you can pass into the function would be along the lines of pd.read_csv('file.csv', skiprows = 801:805)

For more reusable code you can try some of the others on that site like skip_blank_lines which is defaulted to True anyways, so make sure those rows are actually blank in your file.

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.