Skip to main content
St Louis

Back to all posts

How to Drop Nan Values But Not Columns In Pandas?

Published on
3 min read
How to Drop Nan Values But Not Columns In Pandas? image

Best Data Cleaning Tools to Buy in January 2026

1 Cleaning Data for Effective Data Science: Doing the other 80% of the work with Python, R, and command-line tools

Cleaning Data for Effective Data Science: Doing the other 80% of the work with Python, R, and command-line tools

BUY & SAVE
$26.49 $43.99
Save 40%
Cleaning Data for Effective Data Science: Doing the other 80% of the work with Python, R, and command-line tools
2 10Pcs Cell Phone Cleaning Kit, Multifunctional Mini Brushes Cleaner for 15 16 Pro Max Speaker and Receiver, Anti-Clogging Mini Cleaning Dust Remover Tools for Headphones Tablet Computer Camera

10Pcs Cell Phone Cleaning Kit, Multifunctional Mini Brushes Cleaner for 15 16 Pro Max Speaker and Receiver, Anti-Clogging Mini Cleaning Dust Remover Tools for Headphones Tablet Computer Camera

  • ENHANCE AUDIO CLARITY WITH OUR SPECIALIZED PHONE CLEANING TOOL!
  • DURABLE, FLEXIBLE BRUSHES ENSURE EFFECTIVE CLEANING WITHOUT SCRATCHES.
  • COMPACT DESIGN FOR EASY STORAGE; PERFECT FOR ALL HARD-TO-REACH AREAS!
BUY & SAVE
$4.99
10Pcs Cell Phone Cleaning Kit, Multifunctional Mini Brushes Cleaner for 15 16 Pro Max Speaker and Receiver, Anti-Clogging Mini Cleaning Dust Remover Tools for Headphones Tablet Computer Camera
3 Ordilend for iPhone Cleaning Kit for Charging Port Cleaner, Cleaner Kit for AirPod Multi-Tool iPhone Cleaner Repair Lightning Cable for iPad Connector Airpod Speaker Compact Portable with Storage Case

Ordilend for iPhone Cleaning Kit for Charging Port Cleaner, Cleaner Kit for AirPod Multi-Tool iPhone Cleaner Repair Lightning Cable for iPad Connector Airpod Speaker Compact Portable with Storage Case

  • REVIVE YOUR DEVICES: EFFECTIVELY CLEAN PORTS TO FIX SLOW CHARGING ISSUES.

  • ALL-IN-ONE KIT: INCLUDES TOOLS FOR PORTS, SPEAKERS, AND EARBUDS CLEANING.

  • SAFE & PORTABLE: STURDY DESIGN ENSURES SAFETY AND CONVENIENCE ON-THE-GO.

BUY & SAVE
$19.99
Ordilend for iPhone Cleaning Kit for Charging Port Cleaner, Cleaner Kit for AirPod Multi-Tool iPhone Cleaner Repair Lightning Cable for iPad Connector Airpod Speaker Compact Portable with Storage Case
4 PurePort USB-C Multi-Tool Phone Cleaning Kit | Clean Repair & Restore Cell Phone Tablet & Laptop USB C Ports & Cables | Fix Unreliable & Bad Connections | Extend The Life of Your Tech Devices (Black)

PurePort USB-C Multi-Tool Phone Cleaning Kit | Clean Repair & Restore Cell Phone Tablet & Laptop USB C Ports & Cables | Fix Unreliable & Bad Connections | Extend The Life of Your Tech Devices (Black)

  • SAVE HUNDREDS ON REPAIRS-CLEAN & REVIVE YOUR DEVICES WITH PUREPORT!
  • EXTEND YOUR DEVICES' LIFE BY ELIMINATING UNRELIABLE CONNECTIONS EASILY.
  • RESTORE CABLE CONNECTORS AND PORTS WITH OUR SPECIALIZED CLEANING TOOLS!
BUY & SAVE
$24.99
PurePort USB-C Multi-Tool Phone Cleaning Kit | Clean Repair & Restore Cell Phone Tablet & Laptop USB C Ports & Cables | Fix Unreliable & Bad Connections | Extend The Life of Your Tech Devices (Black)
5 Ordilend Keyboard Cleaning Kit Laptop Cleaner, All-in-One Computer Camera Cleaning Kits Brush Tool, Multi-Function PC Electronic Cleaner for iPad iPhone Pro Earbuds Camera Monitor with Patent, Black

Ordilend Keyboard Cleaning Kit Laptop Cleaner, All-in-One Computer Camera Cleaning Kits Brush Tool, Multi-Function PC Electronic Cleaner for iPad iPhone Pro Earbuds Camera Monitor with Patent, Black

  • COMPREHENSIVE KIT: INCLUDES BRUSHES, SPRAYS, AND CLOTHS FOR ALL NEEDS.
  • EFFORTLESS CLEANING: ONE SWIPE RESTORES SCREENS AND KEYBOARDS EFFORTLESSLY.
  • PORTABLE DESIGN: COMPACT AND EASY TO CARRY FOR ON-THE-GO CLEANING.
BUY & SAVE
$19.99 $23.98
Save 17%
Ordilend Keyboard Cleaning Kit Laptop Cleaner, All-in-One Computer Camera Cleaning Kits Brush Tool, Multi-Function PC Electronic Cleaner for iPad iPhone Pro Earbuds Camera Monitor with Patent, Black
6 Keyboard Cleaning Kit Laptop Cleaner, 10-in-1 Computer Screen Cleaning Brush Tool, Multi-Function PC Electronic Cleaner Kit Spray for iPad iPhone Pro, Earbuds, Camera Monitor, All-in-one with Patent

Keyboard Cleaning Kit Laptop Cleaner, 10-in-1 Computer Screen Cleaning Brush Tool, Multi-Function PC Electronic Cleaner Kit Spray for iPad iPhone Pro, Earbuds, Camera Monitor, All-in-one with Patent

  • COMPREHENSIVE KIT: INCLUDES BRUSHES, CLOTHS, AND KEYCAP PULLER TOOLS.

  • EFFORTLESS CLEANING: QUICK SWIPE FOR SPOTLESS KEYBOARDS AND SCREENS.

  • PORTABLE DESIGN: EASY TO CARRY FOR ON-THE-GO CLEANING CONVENIENCE.

BUY & SAVE
$17.99 $19.98
Save 10%
Keyboard Cleaning Kit Laptop Cleaner, 10-in-1 Computer Screen Cleaning Brush Tool, Multi-Function PC Electronic Cleaner Kit Spray for iPad iPhone Pro, Earbuds, Camera Monitor, All-in-one with Patent
7 Cleaner Kit for AirPod, Multi-Tool iPhone Cleaning Kit, Cell Phone Cleaning Repair & Recovery iPhone and iPad (Type C) Charging Port, Lightning Cables, and Connectors, Easy to Store and Carry Design

Cleaner Kit for AirPod, Multi-Tool iPhone Cleaning Kit, Cell Phone Cleaning Repair & Recovery iPhone and iPad (Type C) Charging Port, Lightning Cables, and Connectors, Easy to Store and Carry Design

  • REVIVE YOUR DEVICES: CLEAN PORTS & CONNECTORS FOR RELIABLE CHARGING!
  • ULTIMATE HYGIENE: KEEP YOUR AIRPODS & HEADPHONES DIRT-FREE AND NEW.
  • PORTABLE CONVENIENCE: LIGHTWEIGHT KIT INCLUDES ALL ESSENTIAL TOOLS!
BUY & SAVE
$19.99
Cleaner Kit for AirPod, Multi-Tool iPhone Cleaning Kit, Cell Phone Cleaning Repair & Recovery iPhone and iPad (Type C) Charging Port, Lightning Cables, and Connectors, Easy to Store and Carry Design
+
ONE MORE?

To drop NaN values but not columns in pandas, you can use the dropna() method with the axis parameter set to 0. This will drop rows that contain any NaN values while keeping all columns intact. You can also use the [subset](https://ubuntuask.com/blog/how-to-subset-a-teradata-table-in-python) parameter to specify specific columns to check for NaN values before dropping rows. Additionally, you can use the thresh parameter to set a threshold for the number of non-NaN values a row must have in order to be kept. This allows you to drop rows that have too many NaN values without dropping entire columns.

How to fill missing values in a pandas DataFrame?

There are several ways to fill missing values in a pandas DataFrame. Some common methods include:

  1. Using the fillna() method: The fillna() method allows you to fill missing values with a specific value or using a method like ffill for forward fill or bfill for backward fill.

df.fillna(0) # fill missing values with 0 df.fillna(method='ffill') # fill missing values with the previous non-missing value df.fillna(method='bfill') # fill missing values with the next non-missing value

  1. Using the interpolate() method: The interpolate() method will interpolate missing values based on the values before and after the missing values.

df.interpolate() # interpolate missing values

  1. Using the replace() method: The replace() method allows you to replace specific values in the DataFrame with another value.

df.replace(-999, np.nan) # replace -999 with NaN

  1. Using the dropna() method: If you prefer to simply drop rows with missing values, you can use the dropna() method.

df.dropna() # drop rows with missing values

These are just a few examples of how you can fill missing values in a pandas DataFrame. The best method to use will depend on your specific data and requirements.

How to drop rows with NaN values while keeping a copy of the original DataFrame in pandas?

You can achieve this by creating a copy of the original DataFrame before dropping the rows with NaN values. Here is an example:

import pandas as pd

Creating a sample DataFrame with NaN values

data = {'A': [1, 2, None, 4, 5], 'B': ['foo', 'bar', 'baz', None, 'qux']} df = pd.DataFrame(data)

Creating a copy of the original DataFrame

df_copy = df.copy()

Dropping rows with NaN values from the original DataFrame

df.dropna(inplace=True)

Print the original DataFrame and the copy after dropping NaN values

print("Original DataFrame:") print(df_copy) print("\nDataFrame after dropping NaN values:") print(df)

In this example, the original DataFrame df_copy is created as a copy of the original DataFrame df. The dropna() method is then used to drop rows with NaN values from the original DataFrame df, while the original DataFrame df_copy remains unchanged.

How to drop rows with NaN values in a specific column in pandas?

You can drop rows with NaN values in a specific column in pandas using the dropna() method. You can specify the column using the subset parameter. Here's an example:

import pandas as pd

Create a sample DataFrame

data = {'A': [1, 2, 3, 4], 'B': [5, 6, None, 8], 'C': [9, 10, 11, 12]} df = pd.DataFrame(data)

Drop rows with NaN values in column 'B'

df = df.dropna(subset=['B'])

print(df)

In this example, rows with NaN values in column 'B' will be dropped from the DataFrame.