Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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,我有一个数据帧,如: country indicator y1 y2 y3 y4 0 abc a 0.1 0.1 0.1 0.1 1 abc b 0.1 0.2 0.4 0.1 2 wed a 0.1 0.1 0.1 0.1 3 wed b 0.7 0.1 0.6 0.1 我需要像这样的东西: country y a b

我有一个数据帧,如:

 country  indicator y1  y2    y3   y4
0   abc    a       0.1  0.1   0.1   0.1
1   abc    b       0.1  0.2   0.4   0.1
2   wed    a       0.1  0.1   0.1   0.1
3   wed    b       0.7  0.1   0.6   0.1
我需要像这样的东西:

 country    y         a    b
0   abc    y1       0.1  0.1
           y2       0.1  0.2 
           y3       0.1  0.1  
           y4       0.7  0.1  
1   wed    y1       0.1  0.1
           y2       0.1  0.2 
           y3       0.1  0.1  
           y4       0.7  0.1  
解决方案指向转置,但我似乎无法完全按照需要转换它们

df.set_index(['country','indicator']).stack().unstack(1)
输出:

indicator     a    b
country             
abc     y1  0.1  0.1
        y2  0.1  0.2
        y3  0.1  0.4
        y4  0.1  0.1
wed     y1  0.1  0.7
        y2  0.1  0.1
        y3  0.1  0.6
        y4  0.1  0.1

非常有用。谢谢另一个问题是,我有多个指标,即a、b、c、d、e。。但只需要两个指标a、b。是否有一种方法可以为仅假设的指标a、b设置索引?