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

Python 我可以取消堆栈()中列的子集吗?

Python 我可以取消堆栈()中列的子集吗?,python,pandas,Python,Pandas,我有该格式的每日销售数据 col1 col2 col3 d_1 d_2 d_3 d_4 dog yes US 6 4 2 1 dog no US 4 3 2 5 cat yes US 4 2 0 1 其中d_1、d_2等表示第1天、第2天等的数字。我希望以以下格式的新表结束: col1 col2 col3 day sales dog yes US d_1 6

我有该格式的每日销售数据

col1  col2  col3  d_1  d_2  d_3  d_4
dog   yes   US    6    4    2    1
dog   no    US    4    3    2    5
cat   yes   US    4    2    0    1
其中d_1、d_2等表示第1天、第2天等的数字。我希望以以下格式的新表结束:

col1  col2  col3  day   sales
dog   yes   US    d_1   6
dog   yes   US    d_2   4
dog   yes   US    d_3   2
dog   yes   US    d_4   1
dog   no    US    d_1   4
dog   no    US    d_2   3
dog   no    US    d_3   2
dog   no    US    d_4   5
cat   yes   US    d_1   4
cat   yes   US    d_2   2
cat   yes   US    d_3   0
cat   yes   US    d_4   1

我该怎么做?我找到的最接近的是
df.set_index(['col1','col2','col3']).stack()
,但它仍然没有完成上述工作,它一直将d_1,d_2等视为某种索引,而不是像上面的示例那样将其转换为实际的堆叠数据。

您已经提供了完美的解决方案。堆叠后只需重置索引
并使用
。使用dict重命名
,以获得正确的列标题

df =pd.DataFrame({'col1': {0: 'dog', 1: 'dog', 2: 'cat'},
     'col2': {0: 'yes', 1: 'no', 2: 'yes'},
     'col3': {0: 'US', 1: 'US', 2: 'US'},
     'd_1': {0: 6, 1: 4, 2: 4},
     'd_2': {0: 4, 1: 3, 2: 2},
     'd_3': {0: 2, 1: 2, 2: 0},
     'd_4': {0: 1, 1: 5, 2: 1}})
df_stacked =df.set_index(['col1', 'col2', 'col3']).stack()\
    .reset_index()\
    .rename(columns={'level_3':'day',0:'sales'})
输出

   col1 col2 col3  day  sales
0   dog  yes   US  d_1  6
1   dog  yes   US  d_2  4
2   dog  yes   US  d_3  2
3   dog  yes   US  d_4  1
4   dog   no   US  d_1  4
5   dog   no   US  d_2  3
6   dog   no   US  d_3  2
7   dog   no   US  d_4  5
8   cat  yes   US  d_1  4
9   cat  yes   US  d_2  2
10  cat  yes   US  d_3  0
11  cat  yes   US  d_4  1
它附带了方便的参数来重命名列,使其成为一次重塑和重命名所有列的最简单方法:

value_vars = ['d_1', 'd_2', 'd_3', 'd_4']
id_vars = ['col1', 'col2', 'col3']
df.melt(id_vars, value_vars, var_name='day', value_name='sales')

   col1 col2 col3  day  sales
0   dog  yes   US  d_1      6
1   dog   no   US  d_1      4
2   cat  yes   US  d_1      4
3   dog  yes   US  d_2      4
4   dog   no   US  d_2      3
5   cat  yes   US  d_2      2
6   dog  yes   US  d_3      2
7   dog   no   US  d_3      2
8   cat  yes   US  d_3      0
9   dog  yes   US  d_4      1
10  dog   no   US  d_4      5
11  cat  yes   US  d_4      1

另一种可能性是,如果您有定义良好的存根(
'd_u'
)。在这里,
day
列将只是
'd.
被剥离时的数字

(pd.wide_to_long(df, i=['col1', 'col2', 'col3'], j='day', stubnames=['d'], sep='_')
   .rename(columns={'d': 'sales'})
   .reset_index())

增加可读性,减少代码行数!我会接受的this@BjörnB只有一点点。如果我把所有的
id\u变量
value\u变量
都塞进这个方法中,它不会比你的短很多。最大的好处是它具有用于重命名的自文档化参数。我发现级别_0重命名东西看起来非常不透明,尽管有时有必要:D