generated from Python-2026Spring/assignment-05-final-project-template
19 lines
610 B
Python
19 lines
610 B
Python
|
|
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}")
|