Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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
GroupByDataFrame-Python_Python_Pandas_Numpy - Fatal编程技术网

GroupByDataFrame-Python

GroupByDataFrame-Python,python,pandas,numpy,Python,Pandas,Numpy,如何操作数据帧,以便按项分组,从而使项不会在所有行中重复?输出必须在数据帧中 我整晚都被这件事困住了,完全被困住了 输入 输出 Item Units_Sold Store Holiday Apple 2017-01-01 8 1 Yes 2017-01-02 3 1 No

如何操作数据帧,以便按项分组,从而使项不会在所有行中重复?输出必须在数据帧中

我整晚都被这件事困住了,完全被困住了

输入

输出

Item             Units_Sold       Store      Holiday              
Apple              
  2017-01-01     8                1          Yes   
  2017-01-02     3                1          No
  2017-01-03     8                1          Yes                                      
Pear
  2017-01-01     4                1          Yes
  2017-01-02     3                1          No
  2017-01-03     9                1          Yes                                                        

您可以创建数据帧的目录:

dfs = {i:x.drop('Item',1) for i, x in df.groupby('Item')}
print (dfs)
{'Pear':          Date  Units_Sold  Store Holiday
1  2017-01-01           4      1     Yes
3  2017-01-02           3      1      No
5  2017-01-03           9      1     Yes, 'Apple':          Date  Units_Sold  Store Holiday
0  2017-01-01           8      1     Yes
2  2017-01-02           3      1      No
4  2017-01-03           8      1     Yes}
然后按
键选择:

print (type(dfs['Apple']))
<class 'pandas.core.frame.DataFrame'>

print (dfs['Apple'])
         Date  Units_Sold  Store Holiday
0  2017-01-01           8      1     Yes
2  2017-01-02           3      1      No
4  2017-01-03           8      1     Yes

print (dfs['Pear'])
         Date  Units_Sold  Store Holiday
1  2017-01-01           4      1     Yes
3  2017-01-02           3      1      No
5  2017-01-03           9      1     Yes
并通过以下方式进行选择:

一个稍加改进的解决方案,包括和:


您可以创建数据帧的目录

dfs = {i:x.drop('Item',1) for i, x in df.groupby('Item')}
print (dfs)
{'Pear':          Date  Units_Sold  Store Holiday
1  2017-01-01           4      1     Yes
3  2017-01-02           3      1      No
5  2017-01-03           9      1     Yes, 'Apple':          Date  Units_Sold  Store Holiday
0  2017-01-01           8      1     Yes
2  2017-01-02           3      1      No
4  2017-01-03           8      1     Yes}
然后按
键选择:

print (type(dfs['Apple']))
<class 'pandas.core.frame.DataFrame'>

print (dfs['Apple'])
         Date  Units_Sold  Store Holiday
0  2017-01-01           8      1     Yes
2  2017-01-02           3      1      No
4  2017-01-03           8      1     Yes

print (dfs['Pear'])
         Date  Units_Sold  Store Holiday
1  2017-01-01           4      1     Yes
3  2017-01-02           3      1      No
5  2017-01-03           9      1     Yes
并通过以下方式进行选择:

一个稍加改进的解决方案,包括和:


您需要添加代码。我甚至不确定你想要达到什么目的。您希望输出看起来像什么?就像问题中编码输出部分的第二部分一样,您需要添加代码。我甚至不确定你想要达到什么目的。您希望输出看起来像什么?就像问题中编码输出部分的第二部分
print (df1.xs('Apple', level=1))
         Date  Units_Sold  Store Holiday
0  2017-01-01           8      1     Yes
2  2017-01-02           3      1      No
4  2017-01-03           8      1     Yes

print (df1.xs('Pear', level=1))
         Date  Units_Sold  Store Holiday
1  2017-01-01           4      1     Yes
3  2017-01-02           3      1      No
5  2017-01-03           9      1     Yes
df1 = df.set_index('Item', append=True).swaplevel(0,1).sort_index()
print (df1)
               Date  Units_Sold  Store Holiday
Item                                          
Apple 0  2017-01-01           8      1     Yes
      2  2017-01-02           3      1      No
      4  2017-01-03           8      1     Yes
Pear  1  2017-01-01           4      1     Yes
      3  2017-01-02           3      1      No
      5  2017-01-03           9      1     Yes

print (df1.xs('Apple'))
         Date  Units_Sold  Store Holiday
0  2017-01-01           8      1     Yes
2  2017-01-02           3      1      No
4  2017-01-03           8      1     Yes

print (df1.xs('Pear'))
         Date  Units_Sold  Store Holiday
1  2017-01-01           4      1     Yes
3  2017-01-02           3      1      No
5  2017-01-03           9      1     Yes