송준석
송준석

Reputation: 1031

How do I drop the None line in a csv file using python?

For example, I have the following csv dataset:

[1,2,3,4]
[3,5,2,5]
[,3,2,4]

As you can see in the dataset above, there is a list with None values.

In the above situation, I want to drop the list with None values in csv.

When I tried, I could not even try to erase it because I could not read an empty value.

Please suggest a way to erase it.

here is my tried.

-before i put in xlsx data to variable named data.

while k < cols:
    if data[i] != None:
        with open('data.csv', 'a') as f:
        writer = csv.writer(f)
        writer.writerows(data)
        f.close()

Upvotes: 2

Views: 3789

Answers (1)

sudonym
sudonym

Reputation: 4018

In order to remove rows with 'empty' cells, do this:

1. Import .csv to pandas dataframe

import pandas as pd

df_csv = pd.read_csv('yourfile.csv')

2. Drop NaN rows

df_csv.dropna(axis = 0, how = 'any', inplace = True) 
# axis = 1 will drop the column instead of the row
# how = 'all' will only drop if all cells are NaN

3. Save to .csv

df_csv.to_csv('yourfile_parsed.csv', index = False)

Comments

  1. It is better to refer to None or NaN rather than saying 'empty'
  2. Also, 'clear' better called 'drop' - people otherwise may think you want to remove all values while keeping the row

Upvotes: 6

Related Questions