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
  substring function to create new column Chandan 6 132 Feb-14-2020, 10:11 AM
Last Post: scidam
  Changing Column dtypes in DataFrame Scott 5 220 Jan-23-2020, 02:22 PM
Last Post: perfringo
  Combine a number into integer column and preserv format as number zinho 2 133 Dec-23-2019, 05:02 PM
Last Post: zinho
  itertuples, new column, datetime, pandas karlito 6 370 Nov-29-2019, 11:07 AM
Last Post: karlito
  sort values of a column pandas karlito 2 401 Oct-22-2019, 06:11 AM
Last Post: karlito
  Convert df column to datetime format LarsCordes 2 558 Oct-14-2019, 12:42 PM
Last Post: LarsCordes
  sorting 2D lists by column ToffieFaye 8 351 Sep-25-2019, 07:30 AM
Last Post: ToffieFaye
  Dropping a column from pandas dataframe marco_ita 6 2,002 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 637 Aug-26-2019, 06:36 PM
Last Post: snippsat
  How replace Unnamed header column with previous SriMekala 3 782 Aug-15-2019, 05:10 PM
Last Post: boring_accountant

Forum Jump:


Users browsing this thread: 1 Guest(s)