在索引中搜索pandas数据帧中的值

时间:2017-09-28 17:42:58

标签: python pandas dataframe try-except

    def time_constant_values(time_value, df, band_list):
        try:
            time_value in df.index
        except KeyError:
            print('The time value does not exist!')
        else:
            constants = []
            for band in band_list:
                constants.append(df.loc[time_value][band])
            t0_dict = dict(zip(band_list, constants))
            return t0_dict

pandas数据帧包含格式为hh:mm:ss的索引中的时间值。除了部分之外,我在尝试做错了什么?

我正在尝试访问某些列的特定时间行,然后形成一个字典,其中列标签为键,数据值为值。

谢谢!

1 个答案:

答案 0 :(得分:2)

密钥不存在不会抛出SELECT c_a.constrname AS Constraint_name , t_a.tabname AS ForeignKey_table , i_a.idxname AS ForeignKey_index , t_b.tabname AS PrimaryKey_table , i_b.idxname AS PrimaryKey_index FROM sysreferences AS r INNER JOIN sysconstraints AS c_a ON r.constrid = c_a.constrid INNER JOIN systables AS t_a ON c_a.tabid = t_a.tabid INNER JOIN systables AS t_b ON r.ptabid = t_b.tabid INNER JOIN sysindexes AS i_a ON c_a.idxname = i_a.idxname INNER JOIN sysconstraints AS c_b ON r.primary = c_b.constrid INNER JOIN sysindexes AS i_b ON c_b.idxname = i_b.idxname WHERE t_a.tabname = 'YOUR_TABLE_HERE'; 。它只返回KeyError。始终运行False,然后在对不存在的密钥编制索引时会遇到问题。尝试使用else

if