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

使用Pandas处理CSV后,如何移除DataFrame中所有元素的.0后缀(保留非整数小数)

Great question! The .0 suffix you're seeing happens because when you fill NaN values with -99, pandas converts any integer columns to float (since NaN is a float type by default). Here are a few tailored solutions depending on your data structure:

Option 1: Convert entire columns to integer type (best if all values in the column are whole numbers)

If the columns showing .0 are supposed to be integers (no decimal values like 2.7), you can convert them back to integer dtype. This keeps your data in efficient numeric formats:

import pandas as pd

# Your existing code
data = pd.read_csv(r"\test.csv", sep=';')
data = data.fillna(-99)

# Iterate through columns and convert whole-number float columns to int
for col in data.columns:
    # Check if all values in the column are whole numbers
    if (data[col] % 1 == 0).all():
        data[col] = data[col].astype(int)

Option 2: Convert individual whole-number floats to integers (for mixed columns)

If some columns have a mix of whole numbers (like 202.0) and actual decimals (like 2.7), you can convert only the whole-number floats to integers while leaving decimals as-is. Note this will change the column dtype to object, which is fine for display but may impact performance on large datasets:

def clean_float_values(x):
    # Convert float to int if it's a whole number
    if isinstance(x, float) and x.is_integer():
        return int(x)
    return x

# Apply the function to every element in the DataFrame
data = data.applymap(clean_float_values)

Option 3: Use nullable integer types (prevent float conversion in the first place)

If you know which columns should be integers (even with missing values), you can use pandas' nullable Int64 dtype when reading the CSV. This avoids converting to float entirely:

# Specify dtype for integer columns (replace 'col1', 'col2' with your actual column names)
data = pd.read_csv(r"\test.csv", sep=';', dtype={'col1': 'Int64', 'col2': 'Int64'})
data = data.fillna(-99)

If you're not sure which columns are supposed to be integers, you can combine this with the check from Option 1 after filling:

data = pd.read_csv(r"\test.csv", sep=';')
data = data.fillna(-99)

for col in data.columns:
    if data[col].dtype == float and (data[col] % 1 == 0).all():
        data[col] = data[col].astype('Int64')

Quick note on display-only fixes

If you only care about how the data looks when printed (not the underlying dtype), you can set a custom display format—but this won't change the actual data values:

# This will show whole-number floats without .0, but keep decimals as-is
pd.options.display.float_format = lambda x: '{:.0f}'.format(x) if x.is_integer() else '{:.1f}'.format(x)

To refine this further, it would help to know if the columns with .0 are all intended to be integers, or if some have a mix of whole numbers and decimals. But the above options cover both scenarios!

内容的提问来源于stack exchange,提问作者Omar Fulla

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 09:07:29