2015-07-10 112 views
1

在這個上畫一個空白,應該很簡單。我有一個面板,像這樣:轉置熊貓面板爲列

09-2014 B  35.890847 
      C  32.416667 
      D  25.541903 
10-2014 B  42.654209 
      C  36.350000 
      D  27.931034 
11-2014 B  41.788443 
      C  35.784556 
      D  26.380000 
12-2014 B  37.096036 
      C  34.545515 
      D  26.844368 

展望落得這樣一個數據幀:

  B   C  D 
09-2014 35.890847 32.416667 25.541903 
10-2014 42.654209 ... 

我無法弄清楚如何在矢量變換方式這個數據,任何幫幫我??

謝謝你們

+0

嘗試'df.unstack('。您的數據框是否具有前兩列作爲多級索引? –

回答

1
import pandas as pd 

# replicate your datastructure 
# suppose your dataframe has these three columns with default integer index 
# if your data is pd.Panel() type, then use to_frame() first to convert it to dataframe. 
# ================================================================ 
dates = pd.date_range('2014-09-01', periods=4, freq='MS') 
cat = 'B C D'.split() 
multi_index = pd.MultiIndex.from_product([dates, cat], names=['dates', 'cat']) 
df = pd.DataFrame(np.random.randn(12), columns=['vals'], index=multi_index).reset_index() 

Out[57]: 
     dates cat vals 
0 2014-09-01 B -1.1258 
1 2014-09-01 C 0.9008 
2 2014-09-01 D -0.1890 
3 2014-10-01 B 0.8831 
4 2014-10-01 C -0.2379 
5 2014-10-01 D -0.1837 
6 2014-11-01 B -0.4775 
7 2014-11-01 C -0.6184 
8 2014-11-01 D 0.6763 
9 2014-12-01 B -2.0877 
10 2014-12-01 C -0.3631 
11 2014-12-01 D 0.3132 

# processing 
# ========================================== 
df.set_index(['dates', 'cat']).unstack() 

Out[58]: 
       vals     
cat    B  C  D 
dates        
2014-09-01 -1.1258 0.9008 -0.1890 
2014-10-01 0.8831 -0.2379 -0.1837 
2014-11-01 -0.4775 -0.6184 0.6763 
2014-12-01 -2.0877 -0.3631 0.3132 
+0

非常感謝!我甚至嘗試過堆棧,並且在我看的時候沒有發現無意識的東西!謝謝! – user1610719

1

這似乎是一個經典案例DataFrame.unstack

從那裏例如:)

>>> index = pd.MultiIndex.from_tuples([('one', 'a'), ('one', 'b'), 
...         ('two', 'a'), ('two', 'b')]) 
>>> s = pd.Series(np.arange(1.0, 5.0), index=index) 
>>> s 
one a 1 
    b 2 
two a 3 
    b 4 
dtype: float64 
>>> s.unstack(level=-1) 
    a b 
one 1 2 
two 3 4