你有没有遇到过这样的场景:两个数字相加却变成了字符串拼接?日期列死活不能按月份筛选?一张500MB的表改个类型就缩到50MB?这背后都是数据类型在起作用。本文带你搞懂Pandas中的类型转换,让数据"各归其位"。
文中涉及到的数据文件可以联系我发给你,因为我还不知道怎么在公众号插入文件
数据类型(dtype)决定了数据如何存储、如何计算、占多少内存。选错类型,轻则结果出错,重则内存爆满。
一个直观的例子:
import pandas as pd
# 如果数字被存成了字符串
df = pd.DataFrame({'score': ['85', '92', '78']})
df['score'].mean() # ❌ 报错!字符串没法求平均值把 '85' 存成 85,问题立刻消失。这就是类型转换的意义。
三大理由让你必须关注dtype:
float64 的计算速度远快于 object(Python对象)category 类型的重复字符串比 object 省内存80%以上拿到数据第一件事,看看每列都是什么类型。
import pandas as pd
df = pd.read_csv('data_python/employee_data.csv')
# 方法1:查看所有列的类型
print(df.dtypes)
# 方法2:只看数值列
print(df.select_dtypes(include='number').columns)
# 方法3:只看文本列
print(df.select_dtypes(include='object').columns)
# 方法4:查看某列的具体类型
print(df['age'].dtype)读入CSV后你会发现:age 竟然是 object 类型。因为数据里混入了字符串 'nan',Pandas没法判断它是数值列。
提示:
select_dtypes()的include参数还可以用'int64'、'float64'、'datetime64'、'category'等精确过滤。
astype() 是最直接的类型转换方法,适合干净数据的快速转换。
import pandas as pd
import numpy as np
df = pd.read_csv('data_python/employee_data.csv')
# 将age转为float(先把字符串'nan'替换为真正的NaN)
df['age'] = pd.to_numeric(df['age'], errors='coerce')
# 浮点转整数
df['age_int'] = df['age'].astype('int64')
# 整数转字符串
df['emp_id_str'] = df['emp_id'].astype('str')
# 转布尔:非0为True,0为False
df['high_salary'] = (df['salary'] > 10000).astype(int)
# category类型(节省内存的关键)
df['department'] = df['department'].astype('category')
print(f"department内存占用:{df['department'].memory_usage(deep=True)} bytes")常用type速查表:
'int64''int32' | ||
'float64''float32' | ||
'str''string' | ||
'bool' | ||
'category' | ||
'datetime64[ns]' |
警告:
astype()要求数据本身"干净"。如果列里有非法值(比如数字列混了字母),直接调用会报错。此时需要用pd.to_numeric()。
pd.to_numeric() 是专门为有脏数据的数值列设计的。
# errors='raise':遇到非法值直接报错(默认)
# errors='coerce':非法值变成NaN
# errors='ignore':什么都不做,保持原样
# 最常见的用法:强制转数值,非法值变NaN
df['age'] = pd.to_numeric(df['age'], errors='coerce')
# 看哪些值被强制转成了NaN
invalid_age = df[df['age'].isna() & df['age'].notna() == False]
print(f"无效的age值有 {invalid_age.shape[0]} 个")
# downcast参数:自动缩小数值范围以节省内存
df['age'] = pd.to_numeric(df['age'], errors='coerce', downcast='integer')
df['salary'] = pd.to_numeric(df['salary'], errors='coerce', downcast='float')
print(f"转换后age类型:{df['age'].dtype}")
print(f"转换后salary类型:{df['salary'].dtype}")downcast 参数会自动判断数据范围,选择更小的类型。比如年龄 18~65,int64 可以降到 int8,大幅节省内存。
errors 三兄弟对比:
errors='raise' | ||
errors='coerce' | ||
errors='ignore' |
CSV读入的日期列默认是字符串。你得用 pd.to_datetime() 把它变成真正的日期类型,才能按月汇总、算时间差。
# 基础用法
df['hire_date'] = pd.to_datetime(df['hire_date'])
# format参数:指定格式可大幅提速(尤其10万行以上)
df['hire_date'] = pd.to_datetime(df['hire_date'], format='%Y-%m-%d')
# 处理多种格式混在一起的脏数据
mixed_dates = pd.Series(['2023-01-15', '15/02/2023', '2023.03.20', 'invalid'])
parsed = pd.to_datetime(mixed_dates, format='mixed', errors='coerce')
print(parsed)常见格式代码:
%Y | ||
%y | ||
%m | ||
%d | ||
%H | ||
%M |
提示:如果你的数据日期格式是日/月/年(如
15/02/2023),记得用dayfirst=True。如果年份在前(如2023/02/15),用yearfirst=True。
日期列转成 datetime 后,神奇的事情发生了——你可以通过 .dt 访问器提取任何时间信息。
df['year'] = df['hire_date'].dt.year # 入职年份
df['month'] = df['hire_date'].dt.month # 入职月份
df['day'] = df['hire_date'].dt.day # 入职日
df['weekday'] = df['hire_date'].dt.dayofweek # 周几(0=周一)
df['weekday_name'] = df['hire_date'].dt.day_name() # 周几(中文需设置locale)
df['quarter'] = df['hire_date'].dt.quarter # 第几季度
df['is_leap'] = df['hire_date'].dt.is_leap_year # 是否闰年
# 日期计算:入职多久了?
today = pd.Timestamp('2026-05-16')
df['work_days'] = (today - df['hire_date']).dt.days
df['work_years'] = df['work_days'] / 365.25
print(df[['name', 'hire_date', 'year', 'quarter', 'work_years']].head())日期加减直接用算术运算符:
# 往后推30天
df['probation_end'] = df['hire_date'] + pd.Timedelta(days=30)
# 两个日期列直接相减,得到 timedelta
df['processing_days'] = (pd.to_datetime('2026-01-01') - df['hire_date']).dt.days当一列数据有大量重复值时(比如部门、性别、学历),转成 category 类型能大幅节省内存。
# 转换前:object类型
dept_mem_before = df['department'].memory_usage(deep=True)
# 转换后:category类型
df['department'] = df['department'].astype('category')
dept_mem_after = df['department'].memory_usage(deep=True)
print(f"转换前:{dept_mem_before} bytes")
print(f"转换后:{dept_mem_after} bytes")
print(f"节省:{(1 - dept_mem_after/dept_mem_before)*100:.1f}%")
# category也能排序(需要指定ordered=True)
education_order = ['高中', '大专', '本科', '硕士', '博士']
df['education'] = pd.Categorical(
df['education'],
categories=education_order,
ordered=True
)
# 现在可以按学历排序了
df_sorted = df.sort_values('education')什么时候用 category?
下面用学到的所有技巧完整清洗员工数据。
import pandas as pd
import numpy as np
df = pd.read_csv('data_python/employee_data.csv')
print("清洗前:")
print(df.dtypes)
# ========== 第1步:安全转数值 ==========
df['age'] = pd.to_numeric(df['age'], errors='coerce', downcast='integer')
df['salary'] = pd.to_numeric(df['salary'], errors='coerce', downcast='float')
# ========== 第2步:修正异常值 ==========
# 年龄:有效范围18-80
df.loc[df['age'] < 18, 'age'] = np.nan
df.loc[df['age'] > 80, 'age'] = np.nan
# 工资:去掉明显离谱的(比如月薪不到最低工资或超过50万)
df.loc[df['salary'] < 1000, 'salary'] = np.nan
df.loc[df['salary'] > 500000, 'salary'] = np.nan
# ========== 第3步:转换日期 ==========
df['hire_date'] = pd.to_datetime(df['hire_date'], format='%Y-%m-%d', errors='coerce')
# 添加入职年数和工龄标签
df['hire_year'] = df['hire_date'].dt.year
df['work_days'] = (pd.Timestamp('2026-05-16') - df['hire_date']).dt.days
df['work_years'] = (df['work_days'] / 365.25).round(1)
# ========== 第4步:分类变量优化 ==========
# department转category
df['department'] = df['department'].replace('nan', np.nan)
df['department'] = df['department'].astype('category')
# education设顺序
edu_cat = ['高中', '大专', '本科', '硕士', '博士']
df['education'] = df['education'].replace('nan', np.nan)
df['education'] = pd.Categorical(df['education'], categories=edu_cat, ordered=True)
# gender转category
df['gender'] = df['gender'].astype('category')
# ========== 第5步:验证结果 ==========
print("\n清洗后:")
print(df.dtypes)
print(f"\n总行数:{len(df)}")
print(f"age有值行数:{df['age'].notna().sum()}")
print(f"salary有值行数:{df['salary'].notna().sum()}")
print(f"hire_date有值行数:{df['hire_date'].notna().sum()}")
# 查看各类型内存占用
print("\n内存使用前5列:")
print(df.memory_usage(deep=True).sort_values(ascending=False).head())df.dtypesdf.select_dtypes() | ||
df['col'].astype('int64') | ||
pd.to_numeric(col, errors='coerce') | ||
pd.to_datetime(col, format='...') | ||
col.dt.year/month/day/quarter | ||
+ pd.Timedelta(days=N) | ||
astype('category') |
下一篇我们将深入字符串处理与正则表达式——教你用 .str 访问器一招清洗姓名空格、验证手机号和提取邮箱域名,告别手动逐行处理的噩梦。
#数据类型 #astype #to_datetime #Pandas #category #内存优化
如果这篇文章对你有帮助,欢迎点赞、在看、转发。你的支持是我持续更新的动力!