如何在排除特定值的情况下获取数据框中的最小时间值

How to get the minimum time value in a dataframe with excluding specific value

我有一个格式如下的数据框。我希望获取每一列的最小时间值并将其保存在一个列表中,并排除格式为 (00:00:00) 的特定时间值作为数据帧中任何列中的最小值。

df =  

    10.0.0.155  192.168.1.240   192.168.0.242

0    19:48:46      16:23:40      20:14:07

1    20:15:46      16:23:39      20:14:09

2    19:49:37      16:23:20      00:00:00

3    20:15:08      00:00:00      00:00:00

4    19:48:46      00:00:00      00:00:00

5    19:47:30      00:00:00      00:00:00

6    19:49:13      00:00:00      00:00:00

7    20:15:50      00:00:00      00:00:00

8    19:45:34      00:00:00      00:00:00

9    19:45:33      00:00:00      00:00:00

我尝试使用下面的代码,但它不起作用:

minValues = []

for column in df:

    #print(df[column])

    if "00:00:00" in df[column]:

        minValues.append (df[column].nlargest(2).iloc[-1])

    else:

        minValues.append (df[column].min()) 

print (df)     
   
print (minValues)

想法是将 0 替换为缺失值,然后获得最小时间增量:

df1 = df.astype(str).apply(pd.to_timedelta)

s1 = df1.mask(df1.eq(pd.Timedelta(0))).min()
print (s1)
10.0.0.155      0 days 19:45:33
192.168.1.240   0 days 16:23:20
192.168.0.242   0 days 20:14:07
dtype: timedelta64[ns]

或者获取最小日期时间并最后将输出转换为 HH:MM:SS 值:

df1 = df.astype(str).apply(pd.to_datetime)

s2 = (df1.mask(df1.eq(pd.to_datetime("00:00:00"))).min().dt.strftime('%H:%M:%S')
print (s2)
10.0.0.155       19:45:33
192.168.1.240    16:23:20
192.168.0.242    20:14:07
dtype: object

或到次:

df1 = df.astype(str).apply(pd.to_datetime)

s3 = df1.mask(df1.eq(pd.to_datetime("00:00:00"))).min().dt.time
print (s3)
10.0.0.155       19:45:33
192.168.1.240    16:23:20
192.168.0.242    20:14:07
dtype: object