You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Replace函数将DataFrame中的XNA值替换为NaN?

Replacing 'XNA' Values with NaN in Pandas DataFrame

No problem at all! Since pandas doesn’t natively recognize 'XNA' as a missing value, using the replace() method is the straightforward solution here. Here's how to implement it for your large DataFrame:

Basic Full DataFrame Replacement

First, make sure you have numpy imported (since np.nan is pandas' standard missing value marker):

import pandas as pd
import numpy as np

# Replace all 'XNA' entries with NaN
df.replace('XNA', np.nan, inplace=True)
  • The inplace=True parameter modifies your original DataFrame directly. If you’d rather create a copy instead of altering the original, omit inplace=True and assign the result to a new variable:
    cleaned_df = df.replace('XNA', np.nan)
    

Replace in Specific Columns Only

If you don’t want to replace 'XNA' across all 36 columns (e.g., only in certain columns where it logically represents a missing value), you can target specific columns:

# List of columns where you want to replace 'XNA'
target_cols = ['column_name_1', 'column_name_2']
df[target_cols] = df[target_cols].replace('XNA', np.nan)

Handling Case Variations (Optional)

If your data has case variations like 'xna' or 'Xna', use a regex to catch all instances:

import re

# Case-sensitive exact match
df.replace(r'^XNA$', np.nan, regex=True, inplace=True)

# Case-insensitive match
df.replace(r'^xna$', np.nan, regex=True, flags=re.IGNORECASE, inplace=True)

Once you run this, df.isnull().sum() will correctly count all former 'XNA' values as nulls. This operation is efficient enough for your 1.6M-row DataFrame—pandas handles these vectorized operations quickly even with large datasets.

内容的提问来源于stack exchange,提问作者Nikunj Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:15:07