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

如何从DataFrame中过滤并移除注册日期为2021年的行?

解决方案:移除DataFrame中Regis_date为2021年的数据行

这里提供两种实用的处理方式,可根据你的数据使用场景选择:

方法一:直接基于数字格式判断

你的Regis_date是8位整数格式(如20211230),前四位对应年份,我们可以通过整数除法提取年份,快速过滤掉2021年的数据:

import pandas as pd

# 构造示例DataFrame
data = {
    'MNG': [8059, 8099, 8099, 8099, 8099, 8099, 8960],
    'Cus': [281614530, 2810288123, 2810288123, 2810288123, 2810288123, 2810288123, 2815496326],
    'Regis_date': [20211230, 20211105, 20211106, 20211210, 20220110, 20220124, 20220422]
}
df = pd.DataFrame(data)

# 过滤操作
filtered_df = df[df['Regis_date'] // 10000 != 2021]
print(filtered_df)

执行后输出结果:

MNG          Cus  Regis_date
6  8960  2815496326    20220422

方法二:转换为日期类型后过滤

如果后续还要做更多日期相关操作,建议先将Regis_date转为datetime类型,再基于年份过滤:

import pandas as pd

# 构造示例DataFrame
data = {
    'MNG': [8059, 8099, 8099, 8099, 8099, 8099, 8960],
    'Cus': [281614530, 2810288123, 2810288123, 2810288123, 2810288123, 2810288123, 2815496326],
    'Regis_date': [20211230, 20211105, 20211106, 20211210, 20220110, 20220124, 20220422]
}
df = pd.DataFrame(data)

# 转换为datetime格式
df['Regis_date'] = pd.to_datetime(df['Regis_date'], format='%Y%m%d')
# 过滤2021年数据
filtered_df = df[df['Regis_date'].dt.year != 2021]
# 如需转回原整数格式,执行以下步骤
filtered_df['Regis_date'] = filtered_df['Regis_date'].dt.strftime('%Y%m%d').astype(int)
print(filtered_df)

这个方法扩展性更强,适合有后续日期处理需求的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 00:05:15