Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I drop a column if the last row is nan

Tags:

python

pandas

I have found examples of how to remove a column based on all or a threshold but I have not been able to find a solution to my particular problem which is dropping the column if the last row is nan. The reason for this is im using time series data in which the collection of data doesnt all start at the same time which is fine but if I used one of the previous solutions it would remove 95% of the dataset. I do however not want data whose most recent column is nan as it means its defunct.

A B C
nan t x 
1 2 3
x y z
4 nan 6

Returns

A C
nan x
1 3
x z
4 6
like image 881
JPWilson Avatar asked Oct 17 '20 20:10

JPWilson


People also ask

How do I drop a column with NaN?

Alternatively, you can also use axis=1 as a param to remove columns with NaN, for example df. dropna(axis=1) . Use dropna(axis=0) to drop rows with NaN values from pandas DataFrame.

How can you drop all rows that contains NaN?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .

How do I drop a NaN value in pandas for a specific column?

Pandas DataFrame dropna() Function how: possible values are {'any', 'all'}, default 'any'. If 'any', drop the row/column if any of the values is null. If 'all', drop the row/column if all the values are missing. thresh: an int value to specify the threshold for the drop operation.

How do I change NaN with column mean?

For mean, use the mean() function. Calculate the mean for the column with NaN and use the fillna() to fill the NaN values with the mean.

How do I drop a row with Nan in SQL?

Setting how = ‘all’ – Drops the row or column only if all the values are NaN. how = 'any', for deleting a row if any column has NaN. After dropping the rows, the indexes will not be sequential. reset_index (drop = True) deletes the previous indexes and reorders them.

What is the drop rule for Nan (missing) values in Excel?

1 drop all rows that have any NaN (missing) values. 2 drop only if entire row has NaN (missing) values. 3 drop only if a row has more than 2 NaN (missing) values. 4 drop NaN (missing) in a specific column.

How do I drop NaN values from a Dataframe in Python?

Python / December 7, 2020 Here are 2 ways to drop columns with NaN values in Pandas DataFrame: (1) Drop any column that contains at least one NaN: df = df.dropna (axis='columns')

Is it possible to remove a row if both columns are Nan?

No, you have to set how='all' since OP asked to remove a row if both columns are NaN. Your solution will also remove rows where only one of the two columns contains NaNs.


2 Answers

You can also do something like this

df.loc[:, ~df.iloc[-1].isna()]
    A   C
0   NaN x
1   1   3
2   x   z
3   4   6
like image 155
Agaz Wani Avatar answered Oct 15 '22 13:10

Agaz Wani


Try with dropna

df = df.dropna(axis=1, subset=[df.index[-1]], how='any')
Out[8]: 
     A  C
0  NaN  x
1    1  3
2    x  z
3    4  6
like image 31
BENY Avatar answered Oct 15 '22 15:10

BENY