Reputation: 143
I am reading an excel file using pandas and I'm creating a list of elements for each column. I need to remove or better yet, not read the empty cells at all.
I've tried various combinations of arguments such as:
na_filter=True/False
keep_default_na=True/False
na_values=None
but none of them completely removes the empty cell but only, at best, replaces them with an empty string or nan
Now I'm using this method to do what I need:
df = pd.read_excel('file.xlsx', keep_default_na=False, sheet_name="Sheet1")
a_list = df['A'].tolist()
a_list = list(filter(('').__ne__, a_list))
b_list = df['B'].tolist()
b_list = list(filter(('').__ne__, b_list))
c_list = df['C'].tolist()
c_list = list(filter(('').__ne__, c_list))
that manually removes the empty list elements but I'm looking for a more pythonic way.
Upvotes: 1
Views: 2171
Reputation: 2076
You can use dropna
to remove the row with null values from dataframe. Please refer below code:
import pandas as pd
df = pd.read_excel('file.xlsx')
newdf = df.dropna()
There are few parameters which you can pass to dropna
function based on your requirements.
dataframe.dropna(axis, how, thresh, subset, inplace)
Upvotes: 1