[英]How to convert dictionary of DataFrames into individual DataFrames (Python, Pandas)
I have an original dataframe with 4 columns (for the example lets call them product_id, year_month, week, order_amount) and > 50,000 rows.我有一个原始的 dataframe,有 4 列(例如,我们称它们为 product_id、year_month、week、order_amount)和 > 50,000 行。 There are 240 individual product_id values and each one of them behaves differently in the data, therefore I wanted to create individual dataframes from the original one based on individual product_id.
有 240 个单独的 product_id 值,每个值在数据中的行为都不同,因此我想根据单独的 product_id 从原始数据框创建单独的数据框。 I was able to do this by utilizing:
我能够通过利用来做到这一点:
dict_of_productid = {k: v for k, v in df.groupby('product_id)}
this created a dictionary with the key being the product_id and the values being the columns: product_id, year_month, week, order_amount.这创建了一个字典,键为 product_id,值为列:product_id、year_month、week、order_amount。 Each item in the dictionary also maintained the index from the original df.
字典中的每个项目还维护了原始 df 的索引。 for example: if product_id = dvvd56 was on row# 4035 then on the dictionary it will be on the dataframe created for product_id dvvd56 but with the index still being 4035.
例如:如果 product_id = dvvd56 在 row# 4035 上,那么在字典中它将在为 product_id dvvd56 创建的 dataframe 上,但索引仍然是 4035。
What I'm stuck with now is a dictionary with df's as values but can't find a way to convert these values into individual dataframes I can use and manipulate.我现在坚持的是一个以 df 作为值的字典,但找不到将这些值转换为我可以使用和操作的单个数据帧的方法。 If there is a way to do this please let me know.
如果有办法做到这一点,请告诉我。 I'll be very grateful.
我将不胜感激。 thank you
谢谢你
I found a way to go about this, but I dont know if this is the most appropriate way, but it might help for further answers in order to clarify what I want to do.我找到了一种方法 go 关于这个,但我不知道这是否是最合适的方法,但它可能有助于进一步的答案以澄清我想做什么。
First step was to convert the unique values into a list and then sorting them in order:第一步是将唯一值转换为列表,然后按顺序对它们进行排序:
product_id_list = df['product_id'].value_counts().index.to_list()
product_id_list = sorted(product_id_list)
After this was done I created a formula and then iterated over it with the individual values of the product_id_list:完成此操作后,我创建了一个公式,然后使用 product_id_list 的各个值对其进行迭代:
def get_df(key):
for k in key:
df_productid = dict_of_productid[k]
return df_productid
for c, i in enumerate(product_id_list):
globals()[f'df_{c}'] = get_df([f'{i}'])
this allows me now to separate all the values of the dictionary that was created into separate dataframes that I can call without explicitly stating the product id.这让我现在可以将创建的字典的所有值分离到单独的数据框中,我可以在不明确说明产品 ID 的情况下调用这些数据框。 I can just do
df_1
and get the dataframe.我可以执行
df_1
并获得 dataframe。
(I dont know if this is the most efficient way to go about this) (我不知道这是否是最有效的方式 go 关于这个)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.