1

我正在从许多元素中用 Python 构建一个字典,其中一些是 nan 的,我根本不想将它们添加到字典中(因为那时我会将它插入数据库并且我不想拥有没有意义的字段)。

目前我正在做这样的事情:

data = pd.read_csv("data.csv")

for i in range(len(data)):
    mydict = OrderedDict([("type", "mydata"), ("field2", data.ix[i,2]), ("field5", data.ix[i,5])])

    if not math.isnan(data.ix[i,3]):
        mydict['field3'] = data.ix[i,3]

    if not math.isnan(data.ix[i,4]):
        mydict['field4'] = data.ix[i,4]

    if not math.isnan(data.ix[i,8]):
        mydict['field8'] = data.ix[i,8]

    etc....

是否可以以更扁平的结构完成,即定义我想有条件地插入的字段名称和字段编号数组?

4

3 回答 3

3
>>> fields = [float('nan'),2,3,float('nan'),5]
>>> {"field%d"%i:v for i,v in enumerate(fields) if not math.isnan(v)}
{'field2': 3, 'field1': 2, 'field4': 5}

或者一个有序的字典:

>>> OrderedDict(("field%d"%i,v) for i,v in enumerate(fields) if not math.isnan(v))
OrderedDict([('field1', 2), ('field2', 3), ('field4', 5)])
于 2013-06-20T00:16:22.713 回答
1

这是你要找的吗?

data = pd.read_csv("data.csv")

for i in range(len(data)):
    mydict = OrderedDict([("type", "mydata"), ("field2", data.ix[i,2]), ("field5", data.ix[i,5])])
    # field numbers
    fields = [3,4,8]
    for f in fields:
        if not math.isnan(data.ix[i,f]):
            mydict['field'+str(f)] = data.ix[i,f]
于 2013-06-20T00:08:18.850 回答
1
conditional_fields = ((3, 'field3'), (4, 'field4'), (8, 'field8'))

for i in range(len(data)):
    mydict = OrderedDict([("type", "mydata"), ("field2", data.ix[i,2]), ("field5", data.ix[i,5])])
    for (index, fieldname) in conditional_fields:
        if not math.isnan(data.ix[i, index]):
            mydict[fieldname] = data.ix[i, index]

我假设实际的字段名称不是字面上的“field8”等。

于 2013-06-20T00:08:19.950 回答