简体   繁体   English

Append 基于键值的列表列表

[英]Append list of lists based on key value

I'm converting 2 Dataframes (I will have more dfs) in 2 lists, to group them by a key column "CNPF_UF_MES".我在 2 个列表中转换 2 个数据帧(我将有更多的 dfs),以按键列“CNPF_UF_MES”对它们进行分组。

So I'll have list of lists, each list based on a key value.所以我会有一个列表列表,每个列表都基于一个键值。

But I'm having trouble to append the final result.但是我对 append 的最终结果有问题。

import pandas as pd

df1 = pd.DataFrame({
    'CNPJ_UF_MES': ['1-MG', '1-MG', '2-MG'],
    'CLIENTE_PAR_COMEX': ['1_2_3', '2_3_4', '3_4_5'],
    'REG': ['1110', '1110', '1110'],
    'COD_IP_PAR': ['200', '200', '200'],
    'COD_CLIENTE': ['xxr', 'xxv', 'xxw'],
    'IND_COMEX': ['wer', 'cad', 'sder'],
    'IND_EXTEMP': ['key_1', 'key_2', 'key_3'],
    'DT_INI': ['01032021', '01032021', '01032021'],
    'DT_FIN': ['31032021', '31032021', '31032021'],
    'VALOR': ['wer', 'cad', 'sder'],
    'PIPE_FIN': ['', '', '']
})


df2 = pd.DataFrame({
    'CNPJ_UF_MES': ['1-MG', '1-MG', '1-MG', '1-MG', '2-MG', '2-MG'],
    'CLIENTE_PAR_COMEX': ['1_2_3', '2_3_4', '3_4_5','1_2_3', '2_3_4', '3_4_5'],
    'REG': ['1110', '1110', '1110', '1110', '1110', '1110'],
    'COD_MCAPT': ['C-200', 'C-200', 'C-200', 'C-200', 'C-200', 'C-200'],
    'DT_OP': ['07032021', '07032021', '07032021', '08032021', '08032021', '08032021'],
    'VALOR': ['1', '2', '3', '1', '2', '3'],
    'QTD': ['2', '2', '2', '2', '2', '2'],
    'CNPJ_ADQUI': ['123', '123', '123', '123', '123', '123'],
    'PIPE_FIN': ['', '', '', '', '', '']

})



list1 = df1.values.tolist()
#print(list1)

values = set(map(lambda x:x[0], list1))
newlist1 = [[y for y in list1 if y[0]==x] for x in values]

print(newlist1)

list2 = df2.values.tolist()
#print(list2)


values = set(map(lambda x:x[0], list2))
newlist2 = [[y for y in list2 if y[0]==x] for x in values]

print(newlist2)

##here is my problem:
list3 = newlist1.append(newlist2)
print(list3)

I wish to have lists of lists appended by this key column.我希望有这个关键列附加的列表列表。

Any ideas?有任何想法吗?

This is expected output:这是预期的 output:

[[
  ['2-MG', '3_4_5', '1110', '200', 'xxw', 'sder', 'key_3', '01032021', '31032021', 'sder', ''],
  ['2-MG', '2_3_4', '1110', 'C-200', '08032021', '2', '2', '123', ''],
  ['2-MG', '2_3_4', '1110', 'C-200', '08032021', '2', '2', '123', '']
  ],
 [
  ['1-MG', '1_2_3', '1110', '200', 'xxr', 'wer', 'key_1', '01032021', '31032021', 'wer', ''],
  ['1-MG', '2_3_4', '1110', '200', 'xxv', 'cad', 'key_2', '01032021', '31032021', 'cad', ''],
  ['1-MG', '1_2_3', '1110', 'C-200', '07032021', '1', '2', '123', ''],
  ['1-MG', '2_3_4', '1110', 'C-200', '07032021', '2', '2', '123', ''],
  ['1-MG', '3_4_5', '1110', 'C-200', '07032021', '3', '2', '123', ''],
  ['1-MG', '1_2_3', '1110', 'C-200', '08032021', '1', '2', '123', '']
  ]]

I'll manipulate this lists later and write them in csv, each CSV file would be grouped by "CNPF_UF_MES".稍后我将处理此列表并将它们写入 csv,每个 CSV 文件将按“CNPF_UF_MES”分组。

Regards,问候,

Tanai谷内

The situation where append returns 'None' value was discussed here . 此处讨论了append返回“无”值的情况。

You can improve your code in this way:您可以通过以下方式改进您的代码:

list3 = newlist1 + newlist2 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据单独的“键”列表中的值合并列表 - Merge lists based on value in separate “key” list 根据列表的最后一个值和第一个值在python中附加嵌套列表 - Append nested list in python based on last value and first value of the lists 如何基于Python中的匹配键将键值对添加到另一个字典列表中的现有字典列表中 - How to append key value pair to an existing list of dict from another list of dict based on matching Key in Python 从列表列表创建字典。 如果键存在,则将项目追加到字典值 - Create dictionary from list of lists. Append items to dictionary value if key exist Append 列表到列表列表 - Append lists to list of lists 按值查找列表的键 - Find key of list of lists by value 遍历列表以将值附加到变量列表 - iterating through a list to append a value to variable lists 根据字典内的键值对将字典列表拆分为具有列表长度的列表列表 - Split list of dictionaries into list of lists with list length based on a key value pair inside the dictionary 根据键的值将字典键添加到列表中 - Append a dictionary key to a list depending on the value of the key 如何通过第二个键将列表搜索到嵌套列表中并将其附加到另一个列表中? - How to search lists into a nested list by their second key and append it to another list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM