Home > Mobile >  Removing columns where all values are NaT on Pandas
Removing columns where all values are NaT on Pandas

Time:06-07

I have a dataframe what collects data from an Excel sheet. The problem is, sometimes there are a lot of empty data in the columns, like you can see here in this picture: enter image description here

My question is: how can i remove those the columns where all values are NaT? I know that using my_df = my_df.dropna(axis=1, how='all') won't work and I can't change the data directly on the spreadsheet.

CodePudding user response:

my_df = pd.read_csv("name.csv", na_values=["NaT"]).dropna(axis=1)

Try this, replace the file name with your own. And if there is a header then add header=0. I'm assuming that 'NaT' values are string data, so they are not removed.

It is possible to try to delete in ready dataframe if it is rows.

import pandas as pd

my_df = pd.DataFrame({'a': [1, 2, 'NaT'], 'b': [1, 2, 3], 'c': [1, 2, 3]})


colum = my_df.isin(['NaT']).any()
my_df = my_df.loc[:, ~colum]
  • Related