我正在使用许多元素在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....
可以在更扁平的结构中完成,即定义一个我想条件插入的字段名称和字段编号数组吗?
答案 0 :(得分: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)])
答案 1 :(得分: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]
答案 2 :(得分: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'等。