Python Forum
How to delete column if entire column values are "nan"
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')
Reply
#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']]
Reply
#3
we do not know which column has nan values beforehand, and column names are not the same always.
Reply
#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).
Reply
#5
Thanks a lot!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find duplicates in a pandas dataframe list column on other rows Calab 2 1,901 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,533 Aug-26-2024, 04:52 PM
Last Post: Calab
Question SOLVED: TTP match when final column may or may not be present Calab 1 988 Jul-03-2024, 02:45 PM
Last Post: Calab
  Create new column in dataframe Scott 10 3,339 Jun-30-2024, 10:18 PM
Last Post: Scott
  attempt to split values from within a dataframe column mbrown009 9 5,701 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  Putting column name to dataframe, can't work. jonah88888 2 3,204 Jun-18-2024, 09:19 PM
Last Post: AdamHensley
  Column Transformer with Mixed Types - sklearn aaldb 0 1,254 Feb-22-2024, 03:27 PM
Last Post: aaldb
  concat 3 columns of dataframe to one column flash77 2 2,075 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 2,562 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Finding the median of a column in a huge CSV file markagregory 5 3,161 Jan-24-2023, 04:22 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020