Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Python 熊猫将列融化为行_Python_Pandas_Dataframe_Melt - Fatal编程技术网

Python 熊猫将列融化为行

Python 熊猫将列融化为行,python,pandas,dataframe,melt,Python,Pandas,Dataframe,Melt,我有如下数据: 日期 国家 频道 应用程序购买笔记本电脑 应用程序购买单元 应用程序销售笔记本电脑 应用程序销售单元 网上购买笔记本电脑 网络购买单元 网络销售笔记本电脑 网络销售单元 03/25 美国 电子邮件 6. 10 30 40 5. 7. 9 11 03/25 美国 直接的 1. 30 10 20 6. 8. 10 12 03/25 印度 电子邮件 4. 60 29 37 40 21 43 53 03/25 印度 直接的 11 16 32 22 39 11 7. 45 03/26 美国

我有如下数据:

日期 国家 频道 应用程序购买笔记本电脑 应用程序购买单元 应用程序销售笔记本电脑 应用程序销售单元 网上购买笔记本电脑 网络购买单元 网络销售笔记本电脑 网络销售单元 03/25 美国 电子邮件 6. 10 30 40 5. 7. 9 11 03/25 美国 直接的 1. 30 10 20 6. 8. 10 12 03/25 印度 电子邮件 4. 60 29 37 40 21 43 53 03/25 印度 直接的 11 16 32 22 39 11 7. 45 03/26 美国 电子邮件 6. 10 30 40 5. 7. 9 11 03/26 美国 直接的 1. 30 10 20 6. 8. 10 12 03/26 印度 电子邮件 4. 60 29 37 40 21 43 53 03/26 印度 直接的 11 16 32 22 39 11 7. 45 试试这个:

dfi = df.set_index(['Date','Country','Channel'])

dfi.columns = pd.MultiIndex.from_tuples(dfi.columns.str.split('-', expand=True), 
                                        names=['Category', None, 'Source'])

df_out = dfi.stack([0,2]).reset_index()

df_out
输出:

     Date Country Channel Category  Source  Purchase  Sell
0   03/25      US   Email      App    Cell        10    40
1   03/25      US   Email      App  Laptop         6    30
2   03/25      US   Email      Web    Cell         7    11
3   03/25      US   Email      Web  Laptop         5     9
4   03/25      US  Direct      App    Cell        30    20
5   03/25      US  Direct      App  Laptop         1    10
6   03/25      US  Direct      Web    Cell         8    12
7   03/25      US  Direct      Web  Laptop         6    10
8   03/25   India   Email      App    Cell        60    37
9   03/25   India   Email      App  Laptop         4    29
10  03/25   India   Email      Web    Cell        21    53
11  03/25   India   Email      Web  Laptop        40    43
12  03/25   India  Direct      App    Cell        16    22
13  03/25   India  Direct      App  Laptop        11    32
14  03/25   India  Direct      Web    Cell        11    45
15  03/25   India  Direct      Web  Laptop        39     7
16  03/26      US   Email      App    Cell        10    40
17  03/26      US   Email      App  Laptop         6    30
18  03/26      US   Email      Web    Cell         7    11
19  03/26      US   Email      Web  Laptop         5     9
20  03/26      US  Direct      App    Cell        30    20
21  03/26      US  Direct      App  Laptop         1    10
22  03/26      US  Direct      Web    Cell         8    12
23  03/26      US  Direct      Web  Laptop         6    10
24  03/26   India   Email      App    Cell        60    37
25  03/26   India   Email      App  Laptop         4    29
26  03/26   India   Email      Web    Cell        21    53
27  03/26   India   Email      Web  Laptop        40    43
28  03/26   India  Direct      App    Cell        16    22
29  03/26   India  Direct      App  Laptop        11    32
30  03/26   India  Direct      Web    Cell        11    45
31  03/26   India  Direct      Web  Laptop        39     7
详情:

让我们将日期、国家和频道移动到索引中,现在对其余列使用字符串操作来创建多索引列标题。最后,将类别和源级别堆叠到索引和重置索引中

您可以使用函数from来抽象整形过程(它只是
melt
和一些字符串方法的包装):


您的数据有一个模式:
分类
,然后是
购买
出售
,最后是
。这里的分隔符是
-
names_to
(Category,'.value,'.Source')中的模式将值与
.value
作为列名配对,而其他值则根据位置分为
Category
Source

非常感谢!这起作用了。谢谢你的帮助!
df.pivot_longer(index = ['Date','Country','Channel'], 
                names_to=['Category', '.value', 'Source'], 
                names_sep="-", 
                sort_by_appearance = True)
 
     Date Country Channel Category  Source  Purchase  Sell
0   03/25      US   Email      App  Laptop         6    30
1   03/25      US   Email      App    Cell        10    40
2   03/25      US   Email      Web  Laptop         5     9
3   03/25      US   Email      Web    Cell         7    11
4   03/25      US  Direct      App  Laptop         1    10
5   03/25      US  Direct      App    Cell        30    20
6   03/25      US  Direct      Web  Laptop         6    10
7   03/25      US  Direct      Web    Cell         8    12
8   03/25   India   Email      App  Laptop         4    29
9   03/25   India   Email      App    Cell        60    37
10  03/25   India   Email      Web  Laptop        40    43
11  03/25   India   Email      Web    Cell        21    53
12  03/25   India  Direct      App  Laptop        11    32
13  03/25   India  Direct      App    Cell        16    22
14  03/25   India  Direct      Web  Laptop        39     7
15  03/25   India  Direct      Web    Cell        11    45
16  03/26      US   Email      App  Laptop         6    30
17  03/26      US   Email      App    Cell        10    40
18  03/26      US   Email      Web  Laptop         5     9
19  03/26      US   Email      Web    Cell         7    11
20  03/26      US  Direct      App  Laptop         1    10
21  03/26      US  Direct      App    Cell        30    20
22  03/26      US  Direct      Web  Laptop         6    10
23  03/26      US  Direct      Web    Cell         8    12
24  03/26   India   Email      App  Laptop         4    29
25  03/26   India   Email      App    Cell        60    37
26  03/26   India   Email      Web  Laptop        40    43
27  03/26   India   Email      Web    Cell        21    53
28  03/26   India  Direct      App  Laptop        11    32
29  03/26   India  Direct      App    Cell        16    22
30  03/26   India  Direct      Web  Laptop        39     7
31  03/26   India  Direct      Web    Cell        11    45