Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to delete column if entire column values are "nan"
#1
Hi,
I have read csv file in dataframe, and I want to delete column if entire column values are equal to "nan"


data.csv:

Name    Rank   subname prevname
USA Ca   12     23     nan
USA WD   25     78     nan
RSA      45     nan    nan
not always the entire 3rd row has "nan" values.

I use the below code:
import pandas as pd
df=pd.read_csv(r'data.csv')
 
df.dropna(axis=1, how='all')
Quote
#2
There are at least several ways to do what you want:

# 1
df.drop('prevname', axis=1, inplace=True)

# 2
df = df.iloc[:,:-1] # we assume that prevname is a last column

# 3
df = df[['Name', 'Rank', 'subname']]
Quote
#3
we do not know which column has nan values beforehand, and column names are not the same always.
Quote
#4
(Apr-11-2019, 12:28 PM)Sri Wrote: we do not know which column has nan values beforehand, and column names are not the same always.

In this case, using .dropna is a good choice. You need to pass inplace=True: df.dropna(axis=1, how='all', inplace=True).
snippsat likes this post
Quote
#5
Thanks a lot!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Convert df column to datetime format LarsCordes 2 51 Yesterday, 12:42 PM
Last Post: LarsCordes
  sorting 2D lists by column ToffieFaye 8 129 Sep-25-2019, 07:30 AM
Last Post: ToffieFaye
  Dropping a column from pandas dataframe marco_ita 6 486 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 372 Aug-26-2019, 06:36 PM
Last Post: snippsat
  How replace Unnamed header column with previous SriMekala 3 369 Aug-15-2019, 05:10 PM
Last Post: boring_accountant
  How to modify df column SriMekala 1 550 Aug-14-2019, 11:06 PM
Last Post: boring_accountant
  Add column to numpy matrix Gigux 1 203 Aug-02-2019, 12:05 AM
Last Post: scidam
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 200 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  Change column names from a file Nidhesh 2 299 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 352 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison

Forum Jump:


Users browsing this thread: 1 Guest(s)