generated from Python-2026Spring/assignment-05-final-project-template
feat: 初始化电信客户流失分析项目,完成数据读取和基础清洗
This commit is contained in:
commit
b77ff15e43
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.venv/
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
results/
|
||||||
7044
data/WA_Fn-UseC_-Telco-Customer-Churn.csv
Normal file
7044
data/WA_Fn-UseC_-Telco-Customer-Churn.csv
Normal file
File diff suppressed because it is too large
Load Diff
19
src/main.py
Normal file
19
src/main.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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}")
|
||||||
Loading…
Reference in New Issue
Block a user