在没有pandas的情况下筛选和重排非常大的字典数组

2024-05-15 21:33:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个非常大的字典数组,如下所示:

masterArray =[{'value': '-1', 'product': 'product1', 'Customer': 'customer1', 
'Sensor': 'sensor1', 'Date': '20170302', 'type': 'type1', 'ID': '100'}, 
{'value': '20', 'product': 'product1', 'Customer': 'customer1',  
'Sensor': 'sensor1','Date': '20170302', 'type': 'type2', 'ID': '100'},
{'value': '0', 'product': 'product1', 'Customer': 'customer1',  
'Sensor': 'sensor1', 'Date': '20170302', 'type': 'type1', 'ID': '101'}, 
{'value': '-5', 'product': 'product1', 'Customer': 'customer1',  
'Sensor': 'sensor1', 'Date': '20170302', 'type': 'type2', 'ID': '101'}]

我需要能够打印出每天、产品、传感器和客户的单个csv,第一列作为ID,其余列作为type,值作为行中填充的数据。你知道吗

ID, type1, type2
100, -1, 20
101, 0, -5

我还创建了一个日期集和一个“组合”集来收集产品、传感器和客户的独特日期和组合。你知道吗

不幸的是,我不允许安装pandas库,尽管我认为我要做的是:

df = pd.DataFrame(masterArray)
df.head()
pivot = pd.pivot_table(df, index=['ID'], values=['value'], columns=['type'])


for date in dateset:
#filter for date
    pqd = pivot.query('Date == date')

for row in comboset:
    #filter for each output
    pqc = pqd.query('Customer == row[0] & product == row[1] & sensor == row[2]')

    outputName = str(row[0] + '_' + date + '_' + row[1] + '_' + row[2] + '.csv')
    filepath = os.path.join(path, outputName)
    pqc.to_csv(filepath) #print 

目前,我的想法是将masterArray改成一个巨大的嵌套字典(我自己从其他输入csv文件创建masterArray),但我不确定这是否是最有效的方法。我也不知道如何最好地为这么大的嵌套字典设置逻辑。请帮帮我!你知道吗


Tags: csvidfordatevaluetypecustomersensor
1条回答
网友
1楼 · 发布于 2024-05-15 21:33:55

你可以试试这样的方法:

data_dict = {}
for each in masterArray:
    if not data_dict.has_key(each['ID']):
        data_dict[each['ID']] = []
    data_dict[each['ID']].append({each['type']: each['value']})

相关问题 更多 >