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
  Pandas: summing columns conditional on the column labels ddd2332 0 110 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  matching a row from 2 seperate dataframes and minimg a column randor 0 191 Sep-03-2020, 08:57 PM
Last Post: randor
  Extracting rows based on condition on one column Robotguy 2 146 Aug-07-2020, 02:27 AM
Last Post: Robotguy
  How to shift data frame rows of specified column Mekala 0 368 Jul-21-2020, 02:42 PM
Last Post: Mekala
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 204 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  Pandas DataFrame and unmatched column sritsv19 0 347 Jul-07-2020, 12:52 PM
Last Post: sritsv19
  Assigning Column nunique values to another DataFrame column Pythonito 0 224 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Issue with dataframe column nsadams87xx 0 344 May-29-2020, 02:00 AM
Last Post: nsadams87xx
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 294 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  Add column to CSV using Pandas nsadams87xx 2 426 Apr-15-2020, 08:41 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)