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
  How to print a column name in csv file Truman 1 32 2 hours ago
Last Post: Larz60+
  Counter to keep track how many times values in a list appear in each row in a column chief 0 63 Mar-24-2020, 08:14 PM
Last Post: chief
  add formatted column to pandas data frame alkaline3 0 91 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  DataFrame: To print a column value which is not null out of 5 columns mani 2 130 Mar-18-2020, 06:07 AM
Last Post: mani
  Convert dataframe string column to numeric in Python darpInd 1 189 Mar-14-2020, 10:07 AM
Last Post: ndc85430
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 160 Mar-14-2020, 09:19 AM
Last Post: scidam
  dataframe column mean skip na SriRajesh 0 99 Mar-03-2020, 01:26 PM
Last Post: SriRajesh
  substring function to create new column Chandan 6 216 Feb-14-2020, 10:11 AM
Last Post: scidam
  Changing Column dtypes in DataFrame Scott 5 281 Jan-23-2020, 02:22 PM
Last Post: perfringo
  Combine a number into integer column and preserv format as number zinho 2 174 Dec-23-2019, 05:02 PM
Last Post: zinho

Forum Jump:


Users browsing this thread: 1 Guest(s)