Home > Enterprise >  How to resize a DataFrame dropping columns where NaN values
How to resize a DataFrame dropping columns where NaN values

Time:04-26

I am trying to build a function to recize an existing dataframe into 1 row data frame.

The function looks like below :

def RecizeDF(DataFrame,RowNum):
    NewDF = DataFrame.loc[[RowNum]]
    NewDF = NewDF.reset_index()
    NewDF = NewDF.dropna(how='all',axis=0)
    return NewDF

But when I print :

print(RecizeDF(DF,2))

it does return :

   Unnamed: 2  Unnamed: 3  ...  Unnamed: 1266  Unnamed: 1267
0  20170428.0  20170531.0  ...            NaN            NaN

So we can see that column with NaN Values aren't removed.

How would you remove columns where values are NaN ?

I tried to add inplace = True but there it does return None

The output would be the following :

   Unnamed: 2  Unnamed: 3  ...  
0  20170428.0  20170531.0  ...  

CodePudding user response:

You should change the axis in your dropna.

axis=0 is index axis, then you request the whole row to be None to be removed.

axis=1 is columns axis, it shall fit your desired behaviour.

  • Related