telco-customer-churn-predic.../src/main.py

19 lines
610 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import polars as pl
data_path = "C:/Users/s1313/Desktop/telco_churn_analysis/data/WA_Fn-UseC_-Telco-Customer-Churn.csv"
try:
df = pl.read_csv(data_path)
# 仅当TotalCharges是字符串类型时才处理
if df["TotalCharges"].dtype == pl.Utf8:
df = df.with_columns(
pl.col("TotalCharges").str.replace(" ", "0").cast(pl.Float64, strict=False)
)
print("✅ 数据处理完成!")
print(f"TotalCharges类型{df['TotalCharges'].dtype}")
print("\n前2行预览")
print(df.head(2))
except Exception as e:
print(f"❌ 操作失败:{e}")