如何在pandas中获取函数中创建的动态数据帧

2024-09-28 21:32:28 发布

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

我有以下python中的主要数据帧

   ID       Date          Product_Name     transaction
   1        01-01-2018    MS               234
   1        01-02-2018    MS               2345
   1        02-02-2018    MS               234
   2        01-01-2018    HSD              2312
   2        02-02-2018    HSD              2234
   3        01-01-2018    MS               2123
   3        02-02-2018    MS               345

现在,我想基于IDProduct_Name对数据帧进行子集,这更具动态性。因此,从上面的数据帧中,它将创建3个动态数据帧(基于IDProduct_Name列中的唯一值),并使用以下子集过滤器将其命名为MS_1HSD_2MS_3

   ID = 1 and Product_Name = 'MS'
   ID = 2 and Product_Name = 'HSD'
   ID = 3 and Product_Name = 'MS'

有可能在熊猫身上做吗


Tags: and数据nameid过滤器dateproduct命名
1条回答
网友
1楼 · 发布于 2024-09-28 21:32:28

更好的方法是创建dictionary of DataFrames:

d = {'{}_{}'.format(k[1], k[0]):v for k, v in df.groupby(['ID','Product_Name'])}
print (d)
{'MS_1':    ID        Date Product_Name  transaction
0   1  01-01-2018           MS          234
1   1  01-02-2018           MS         2345
2   1  02-02-2018           MS          234, 'HSD_2':    ID     Date Product_Name  transaction
3   2  01-01-2018          HSD         2312
4   2  02-02-2018          HSD         2234, 'MS_3':    ID     Date Product_Name  transaction
5   3  01-01-2018           MS         2123
6   3  02-02-2018           MS          345}

以及选择使用:

print (d['MS_1'])
   ID        Date Product_Name  transaction
0   1  01-01-2018           MS          234
1   1  01-02-2018           MS         2345
2   1  02-02-2018           MS          234

print (d['HSD_2'])
   ID        Date Product_Name  transaction
3   2  01-01-2018          HSD         2312
4   2  02-02-2018          HSD         2234

编辑:

@jpp建议按元组创建密钥:

d = dict(tuple(df.groupby(['ID','Product_Name'])))
print (d)
{(1, 'MS'):    ID        Date Product_Name  transaction
0   1  01-01-2018           MS          234
1   1  01-02-2018           MS         2345
2   1  02-02-2018           MS          234, (2, 'HSD'): ID   Date Product_Name  transaction
3   2  01-01-2018          HSD         2312
4   2  02-02-2018          HSD         2234, (3, 'MS'):  ID    Date Product_Name  transaction
5   3  01-01-2018           MS         2123
6   3  02-02-2018           MS          345}


print (d[(1, 'MS')])
   ID        Date Product_Name  transaction
0   1  01-01-2018           MS          234
1   1  01-02-2018           MS         2345
2   1  02-02-2018           MS          234

print (d[(2, 'HSD')])
   ID        Date Product_Name  transaction
3   2  01-01-2018          HSD         2312
4   2  02-02-2018          HSD         2234

对于使用所有子数据帧转换回大数据帧,请使用^{}

df = pd.concat(dict(tuple(df.groupby(['ID','Product_Name'])))).reset_index(drop=True)
print (df)
   ID        Date Product_Name  transaction
0   1  01-01-2018           MS          234
1   1  01-02-2018           MS         2345
2   1  02-02-2018           MS          234
3   2  01-01-2018          HSD         2312
4   2  02-02-2018          HSD         2234
5   3  01-01-2018           MS         2123
6   3  02-02-2018           MS          345

相关问题 更多 >