sneha nair
sneha nair

Reputation: 26

How to remove blank and null values from a list that passed into dataframe in python

I have dataframe column like below.

df['lane']
AZ
NL

NaN
BL
AZ

My code

unique_lane = df['lane'].unique()
unique_lane = pd.DataFrame( list(zip(unique_lane)), columns =['unique_lane'])
t = ', '.join(unique_lane['unique_lane'].astype(str))

While I am passing unique list values blank('') or Null values should be removed from the list. The list t created should contain not blank or not Null values.

bigdata_null_zones = bigdata_null_zones[~bigdata_null_zones["lane"].isin([t])]

How can this be done in python?

Upvotes: 0

Views: 1132

Answers (1)

jezrael
jezrael

Reputation: 862396

Sample data for test DataFrame from question:

df = pd.DataFrame({'lane':['AZ','NL','', np.nan, 'BL','AZ']})

Test for pass only misisng values or empty strings:

df = pd.DataFrame({'lane':['', np.nan]})
print (df)
  lane
0     
1  NaN

bigdata_null_zones = pd.DataFrame({'lane':['AZ','NL','AB', 'BL','AZ']})
print (bigdata_null_zones)
  lane
0   AZ
1   NL
2   AB
3   BL
4   AZ

After remove it get empty Series:

t = df['lane'].replace('',np.nan).dropna()
print (t)
Series([], Name: lane, dtype: float64)

So if pass get same values, because nothing filtered:

bigdata_null_zones[bigdata_null_zones["lane"].isin(t)]
print (bigdata_null_zones)
  lane
0   AZ
1   NL
2   AB
3   BL
4   AZ

If same DataFrame:

df = pd.DataFrame({'lane':['AZ','NL','', np.nan, 'BL','AZ'],
                   'col':range(6)})

print (df)
  lane  col
0   AZ    0
1   NL    1
2         2
3  NaN    3
4   BL    4
5   AZ    5

df1 = df.assign(lane= df['lane'].replace('',np.nan)).dropna(subset=['lane'])
print (df1)
  lane  col
0   AZ    0
1   NL    1
4   BL    4
5   AZ    5

Upvotes: 1

Related Questions