Stanleyrr
Stanleyrr

Reputation: 875

How to force Pandas "read_csv" function to keep blank values

I have a csv containing 1 column and 2302 rows, 8 of which are blank. By blank I mean they are completely blank (i.e. no spaces or anything).

When I read the csv into Pandas dataframe using the Python code below, the output "judg_count" got truncated to 2294 rows (i.e. the 8 rows with blank values were automatically removed) as opposed to 2302 rows.

judg_count=pd.read_csv('sample_data.csv')

I have tried the multiple codes below:

judg_count=pd.read_csv('sample_data.csv').fillna(' ')

judg_count=pd.read_csv('sample_data.csv').replace('',np.nan)

judg_count = pd.read_csv('sample_data.csv', na_filter= False)

judg_count = pd.read_csv('sample_data.csv').fillna(value = 0)

Unfortunately none of them worked because when I called the "judg_count" variable it still always returned 2294 rows with the blank rows automatically removed.

My question is is there a way to force Pandas to preserve those blank rows when reading the csv?

Below is a screenshot of some of the rows in my CSV. Note the blank value at cell #25:

enter image description here

Upvotes: 2

Views: 2508

Answers (1)

Simon Crane
Simon Crane

Reputation: 2182

There is a keyword you can pass:

judg_count=pd.read_csv('sample_data.csv', skip_blank_lines=False)

You can read more in the docs here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html

Upvotes: 4

Related Questions