Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 有没有办法从三个单独的for循环中合并三个dict?_Python_Python 3.x_Pandas_Dictionary - Fatal编程技术网

Python 有没有办法从三个单独的for循环中合并三个dict?

Python 有没有办法从三个单独的for循环中合并三个dict?,python,python-3.x,pandas,dictionary,Python,Python 3.x,Pandas,Dictionary,我正在使用Pandas根据CSV文件中三个独立列的数据创建三个列表。我的名单是: order_list = df['order'][:10].tolist()) user_id_list = df['user_id'][:10].tolist()) company_id_list = df['company_id'][:10].tolist()) 在我的create\u order()函数中,我循环遍历每个列表,并使用enumerate()将项目添加到三个单独的目录中。例如: def cre

我正在使用Pandas根据CSV文件中三个独立列的数据创建三个列表。我的名单是:

order_list = df['order'][:10].tolist()) 
user_id_list = df['user_id'][:10].tolist())
company_id_list = df['company_id'][:10].tolist())
在我的
create\u order()
函数中,我循环遍历每个列表,并使用
enumerate()
将项目添加到三个单独的目录中。例如:

def create_order(orders, users, companies):

    for index, order in enumerate(orders):
        x = {
            'name': order,
        }
    for index, user_id in enumerate(users):
        y = {
            'user': user_id,
        }
    for index, company_id in enumerate(companies):
        z = {
            'company': company_id,
        }

if __name__ == '__main__':
    create_order(order_list, user_id_list, company_id_list)
我想将所有三个DICT合并在一起,并从CSV文件返回每行数据的数据。但是,我需要
顺序
对象是唯一的。我已经能够合并使用返回的dicts。e、 g.
{**x,**y}
。如果我使用嵌套for循环,那么我可以通过这种方式完成dict的合并;这阻止我返回唯一的订单

最好的方法是将我的字典合并成一个dict,这样我就可以循环并返回具有唯一顺序的唯一数据

编辑/更正 这是我的原始DF输出:

     company_id                 user_id          order
  0         111                     222           order 1
  1         111                     222           order 1
  2         111                     222           order 1
  3         111                     222           order 2
  4         111                     222           order 2
  5         111                     222           order 3
以下是我对所有唯一订单的期望输出示例:

company_id   user_id               order
       111       222             order 1
       111       222             order 2
       111       222             order 3
尝试解决for循环问题的原因是需要所有所需的输出返回dict,以便通过googledfpapi导入数据库

下面是在@MeHdi的指导下具有所需输出的工作代码

df = pd.read_csv('order_data.csv')

order_list = df['order'].tolist()
user_id_list = df['user_id'].tolist()
company_id_list = df['company_id']].tolist()

for x in company_id_list:
    company_id = int(x)

    df = pd.DataFrame(
        {
            'name': order_list,
            'userId': user_id_list,
            'companyId': company_id
        }
    )

 list_obj = df.reindex(columns=['companyId', 'userId', 'name']).drop_duplicates()

 records = list_obj.to_dict('records')
 print(records)

这将返回上面列出的所需输出

您可以使用原始数据帧。在数据帧上使用reindex和drop_duplicates函数以获得所需的结果

df = pd.DataFrame({'order':['order1', 'order2', 'order3', 'order3'], 'user_id':[222, 222, 222, 222], 'company_id':[111, 111, 111, 111], 'column_d':['a', 'b', 'c', 'd']})

print(df)

df.reindex(columns=['company_id', 'user_id', 'order']).drop_duplicates()

您可以在原始数据帧上工作。在数据帧上使用reindex和drop_duplicates函数以获得所需的结果

df = pd.DataFrame({'order':['order1', 'order2', 'order3', 'order3'], 'user_id':[222, 222, 222, 222], 'company_id':[111, 111, 111, 111], 'column_d':['a', 'b', 'c', 'd']})

print(df)

df.reindex(columns=['company_id', 'user_id', 'order']).drop_duplicates()

相对于所示的输出,您的输入看起来是什么样的?您可能不需要在三个列表中分开,而需要对循环执行三种不同的操作。发布原始的
df
相对于显示的输出,您的输入看起来是什么样的?您可能不需要在三个列表中分开,并且为循环执行三种不同的操作。发布原始的
df
谢谢!我早些时候尝试过drop_duplicates方法,但没有成功。我不知道reindex-这正是我需要的。太好了。当然,reindex首先删除您不需要的列(或获取您需要的列),然后对新选定的列应用drop_duplicates,得到您想要的结果。祝你好运,谢谢!我早些时候尝试过drop_duplicates方法,但没有成功。我不知道reindex-这正是我需要的。太好了。当然,reindex首先删除您不需要的列(或获取您需要的列),然后对新选定的列应用drop_duplicates,得到您想要的结果。祝你好运