2

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()
4
  • 2
    What format is that dataset in? Is that what the file looks like? Does the file have those brackets? Please create a minimal reproducible example Commented Jun 7, 2018 at 3:33
  • What code have you tried? Commented Jun 7, 2018 at 3:34
  • 1
    Show what you have tried. Besides being unclear what you are asking, there is no really specific problem statement. Commented Jun 7, 2018 at 3:34
  • What modules are you using? I suggest using pandas for csv. It offers a lot more versatility than just reading the file normally. Commented Jun 7, 2018 at 3:54

1 Answer 1

6

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
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with all comments to the question above. However, when my reputation was below 68, it always annoyed me tremendously that people won't simply provide the answer if they know it already rather than trying to improve my question. We all started from somewhere.

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.