PANDAS将数据框按唯一值行拆分为多个 [英] PANDAS split dataframe to multiple by unique values rows
本文介绍了PANDAS将数据框按唯一值行拆分为多个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Pandas中有一个DataFrame
I have a DataFrame in Pandas
PRICE Name PER CATEGORY STORENAME
0 9.99 MF gram Indica Store1
1 9.99 HY gram Herb Store2
2 9.99 FF gram Herb Store2
我想要做的是将它们分成多个数据帧以具有唯一的名称,然后将其分成类别.
What I want to do is split these into multiple data frames to have unique names, then in those split to category.
当前代码:
names = df['STORENAME'].unique().tolist() #find unique values
store1 = df[df['STORENAME']==names[0]]
store2 = df[df['STORENAME']==names[1]]
此代码可以完美运行,但是我想知道是否存在Python方式,因为商店数量可能会发生变化.
This code works perfectly but I am wondering if there is a Pythonic way since the number of stores may change.
这是用来绘制商店中类别价格差异的图.
This is needed to plot the difference in prices in categories in stores.
谢谢!
推荐答案
我认为您可以创建dictionary of DataFrames
:
dfs = dict(tuple(df.groupby('STORENAME')))
然后通过STORENAME
选择:
store1 = dfs['Store1']
store2 = dfs['Store2']
print (store1)
PRICE Name PER CATEGORY STORENAME
0 9.99 MF gram Indica Store1
print (store2)
PRICE Name PER CATEGORY STORENAME
1 9.99 HY gram Herb Store2
2 9.99 FF gram Herb Store2
这篇关于PANDAS将数据框按唯一值行拆分为多个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文