Pandas - 基于多个标准过滤掉,一列包含重复

问题描述 投票:0回答:1

有一些错误的麻烦。如果满足两个条件,我正在尝试过滤掉数据。

import pyodbc
import pandas as pd
import datetime
from dateutil.relativedelta import relativedelta

effdate = datetime.date(2018,12,31)
conn = pyodbc.connect(
    r'DRIVER={ODBC Driver 13 for SQL Server};'
    r'SERVER=server;'
    r'DATABASE=database;'
    r'Trusted_Connection=yes;'
    )
strSQL = "" # here is a SQL query which pulls many columns, including SaleDate, which is date format, and CategoryName, which contains text

df_auction = pd.read_sql(strSQL, conn)
priordate_rt = effdate + relativedelta(months=-6)
priordate_rt = pd.Timestamp(priordate_rt)
df_auction['SaleDateAdj'] = pd.to_datetime(df_auction['SaleDate'], format='%Y-%m-%d')
df_auction = df_auction[~((df_auction['CategoryName']=='Cars') & (df_auction['SaleDateAdj']<priordate_rt))]

TypeError:'str'和'int'实例之间不支持'<'

我可以告诉你,这本身就有效:

df_test = df_auction[(df_auction['SaleDateAdj']<priordate_rt)]

这一行本身给了我ValueError:无法从重复的轴重新索引。

df_test = df_auction[(df_auction['CategoryName']=='Cars')]
python pandas
1个回答
1
投票

试着做

import pandas as pd
df_test['SaleDate'] = pd.to_datetime(df_test['SaleDate'])

然后执行比较以过滤掉。

© www.soinside.com 2019 - 2024. All rights reserved.