Marcos Dias
Marcos Dias

Reputation: 450

Removing columns where all values are NaT on Pandas

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.

Upvotes: 0

Views: 1099

Answers (1)

inquirer
inquirer

Reputation: 4803

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]

Upvotes: 1

Related Questions