Abrar Abrar -4 years ago 44
Python Question

How to remove Empty Cell from data frame row wise

I have csv data in the following format

ab aback abandon abate Class
ab NaN abandon NaN A
NaN aback NaN NaN A
NaN aback abandon NaN B
ab NaN NaN abate C
NaN NaN abandon abate C


I want to remove the NaN cell and re-arrange the data as

ab abandon A
aback A
aback abandon B
ab abate C
abandon abate C


The headers are not required in the processed form. I tried a number of threads such as Remove NaN from pandas series, Missing Data In Pandas Dataframes, How can I remove Nan from list Python/NumPy etc. but they are all providing column wise solution.

here is the sample file.
It has empty cells and when I display it using dataframe, all the empty cells are shown as NaN
Here is the code

import pandas as pd

df = pd.read_csv('C:/Users/ABRAR/Google Drive/Tourism Project/Small_sample.csv', low_memory=False)
print(df)

Answer Source
df = df.apply(lambda x: sorted(x.values.astype(str)), axis=1)\
   .replace('nan','')

df = df.drop(df.index[df.eq('').all(axis=1)]) #drop all null rows

df = df.drop(df.columns[df.eq('').all()],axis=1) #drop all null columns

print(df.head())

Output:

         ab   aback
14    access        
18    accept        
23    access        
24      able  accept
47  accepted        
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download