从一列中的唯一值创建Pandas DataFrames [英] Create Pandas DataFrames from Unique Values in one Column

查看:67
本文介绍了从一列中的唯一值创建Pandas DataFrames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有1000行的Pandas数据框. Names列包含客户名称及其记录.我想根据每个客户的唯一名称创建单独的数据框.我把唯一的名字放在列表中

I have a Pandas dataframe with 1000s of rows. and it has the Names column includes the customer names and their records. I want to create individual dataframes for each customer based on their unique names. I got the unique names into a list

customerNames = DataFrame['customer name'].unique().tolist()这给出了以下数组

['Name1', 'Name2', 'Name3, 'Name4']

我尝试了一个循环,方法是捕获上面列表中的唯一名称,并为每个名称创建数据框,然后将数据框分配给客户名称.因此,例如,当我编写Name3时,它应该将Name3的数据作为单独的数据帧

I tried a loop by catching the unique names in the above list and creating dataframes for each name and assign the dataframes to the customer name. So for example when I write Name3, it should give the Name3's data as a separate dataframe

for x in customerNames:
    x = DataFrame.loc[DataFrame['customer name'] == x]
x

以上几行只返回了Name4的数据帧作为数据帧结果,但跳过了其余部分.

Above lines returned the dataframe for only Name4 as dataframe result, but skipped the rest.

我该如何解决这个问题?

How can I solve this problem?

推荐答案

您的当前迭代每次运行都会覆盖x两次:for循环将客户名称分配给x,然后分配一个数据框

Your current iteration overwrites x twice every time it runs: the for loop assigns a customer name to x, and then you assign a dataframe to it.

要稍后能够按名称调用每个数据框,请尝试将它们存储在字典中:

To be able to call each dataframe later by name, try storing them in a dictionary:

df_dict = {name: df.loc[df['customer name'] == name] for name in customerNames}

df_dict['Name3']

这篇关于从一列中的唯一值创建Pandas DataFrames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆