我正在尝试删除具有datetime索引列的数据帧中的行。使用<;将str与int进行比较时出错
下面是我正在运行的代码
def clean(df):
for i in range(len(df)):
hour = pd.Timestamp(df.index[i]).hour
minute = pd.Timestamp(df.index[i]).minute
if hour < 8 and minute < 45:
df.drop(axis=1, index=i, inplace=True)
导致错误:TypeError: '<' not supported between instances of 'str' and 'int'
如果我单独写一行:type(pd.Timestamp(df.index[i]).hour)
它返回<class 'int'>
我可以执行类似于hour += 1
的数学运算,但是当比较小时或分钟时,if语句返回错误。将代码更改为hour = int(pd.Timestamp(df.index[i]).hour)
也没有帮助
谢谢
不用在行上循环(这会很慢),只需制作一个掩码,指定要保留哪些行,并让
pandas
给出一个(更快的)答案:相关问题 更多 >
编程相关推荐