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

如何判断DataFrame列类型(数值/数值分类/分类)以实现缺失填充?

Great question! Let's break this down step by step, using pandas since you're working with DataFrames.

1. How to check if a column is a categorical column?

In pandas, there are two reliable ways to identify categorical columns:

  • Check dtype directly: Use df['column_name'].dtype == 'category'. This works if the column was explicitly set as categorical (e.g., with astype('category')).
  • Use pandas' type checking utility: pd.api.types.is_categorical_dtype(df['column_name']) is more robust, especially if dealing with nullable categorical types or edge cases.

For example:

import pandas as pd
df = pd.DataFrame({'b': [1,2,3]})
df['b'] = df['b'].astype('category')

# Both will return True
print(df['b'].dtype == 'category')
print(pd.api.types.is_categorical_dtype(df['b']))

If you're dealing with logical categorical columns (values are discrete numbers but not set to category dtype yet), you'll need to rely on business context or check if the number of unique values is small relative to the total rows (e.g., len(df['b'].unique()) / len(df) < 0.1 could indicate a categorical column).

2. How to distinguish between numeric columns and numeric categorical columns?

A numeric categorical column is a categorical column where the underlying category values are numeric. Here's how to tell them apart:

Case 1: Explicit categorical columns (pandas dtype is 'category')

  • First confirm it's a categorical column using the methods above.
  • Then check if the category values are numeric:
    # Check if the categories are numeric
    is_numeric_category = pd.api.types.is_numeric_dtype(df['b'].cat.categories)
    
    If this returns True, you've got a numeric categorical column.

Case 2: Logical numeric categorical columns (not set to 'category' dtype)

These are columns with numeric values that represent discrete categories (like your b column with values 1,2,3). To identify them:

  • Check if the values are discrete (small number of unique values)
  • Verify with your business logic (e.g., "b represents user tiers 1-3, so it's a category")

Numeric columns (non-categorical)

These are columns with continuous or unbounded numeric values (like your a column with 1.35, 2.42). Use pd.api.types.is_numeric_dtype(df['column_name']) to confirm—this will return True for int, float, and nullable numeric types, but False for categorical columns even if their categories are numeric.

Bonus: Implementing your missing index filling logic

Since you mentioned filling missing indices (with forward fill for categorical columns), here's a quick implementation:

import numpy as np

# Example DataFrame with missing index (3)
df = pd.DataFrame({'a': [1.35, 2.42, np.nan], 'b': [1,2,3]}, index=[1,2,4])
df['b'] = df['b'].astype('category')

# Reindex to include all consecutive indices
full_index = pd.Index(range(df.index.min(), df.index.max() + 1))
df = df.reindex(full_index)

# Fill missing values: forward fill for categoricals, keep NaN for numeric
for col in df.columns:
    if pd.api.types.is_categorical_dtype(df[col]):
        df[col] = df[col].ffill()
    # For numeric columns, you can choose to fill with NaN, mean, etc.
    # else:
    #     df[col] = df[col].fillna(np.nan)

print(df)

This will give you:

a  b
1  1.35  1
2  2.42  2
3   NaN  2
4   NaN  3

内容的提问来源于stack exchange,提问作者Yunus Emrah Uluçay

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 17:12:42