0

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?

1 Answer 1

1

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

9 Comments

bigdata_null_zones =bigdata_null_zones[~bigdata_null_zones["lane"].isin([t])]. Will this line of code break if I have no values inside t beacuse df[lane] had only blank or null values
@s - Unfortunately not understand.
My orginal daframe df['lane'] has only null and blank values only. then I apply your code remove those using code t = df['lane'].replace('',np.nan).dropna(). After this I pass this inside the second dataframe. I am asking in such a case will the code break
@snehanair - I can test, but I think not.
@jazrael, In that case t doesnt have any value so will the isin condition work Thats my question
|

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.