2015-03-31 57 views
0

我想在python中取消一列,但它並不完全符合我的期望。我的表(稱爲DF)類似於此:在Python中使用Unstack

station_id year  Day1 Day2 
210018  1916  4  7 
       1917  3  9 
256700  1916  NaN  8 
       1917  6  9 

我想在今年讓每stationn一年所有的日子都在一行中拆散。 1916年那兩天會再啓動第一,隨後2天,從1917年的210018臺和256700.

一個例子是這樣的:

station_id   1916  1917 
210018    4 7  3 9 
256700    NaN 8  6 9 

我想使用此代碼:

df2=df.unstack(level='year') 
df2.columns=df2.columns.swaplevel(0,1) 
df2=df2.sort(axis=1) 

我收到一個錯誤,說AttributeError: 'Series' object has no attribute 'columns'

任何幫助,將不勝感激。

回答

3

你需要讓year索引你叫拆散

try: 
    # for Python2 
    from cStringIO import StringIO 
except ImportError: 
    # for Python3 
    from io import StringIO 

import pandas as pd 


text = '''\ 
station_id year  Day1 Day2 
210018  1916  4  7 
210018  1917  3  9 
256700  1916  NaN  8 
256700  1917  6  9''' 

df = pd.read_table(StringIO(text), sep='\s+') 
df = df.set_index(['station_id', 'year']) 
df2 = df.unstack(level='year') 
df2.columns = df2.columns.swaplevel(0,1) 
df2 = df2.sort(axis=1) 
print(df2) 

產生

year  1916  1917  
      Day1 Day2 Day1 Day2 
station_id      
210018  4 7 3 9 
256700  NaN 8 6 9 

然而,如果year是列,而不是一個索引,那麼

df = pd.read_table(StringIO(text), sep='\s+') 
df = df.set_index(['station_id']) 
df2 = df.unstack(level='year') 
df2.columns = df2.columns.swaplevel(0,1) 
df2 = df2.sort(axis=1) 

潛在客戶到AttributeError: 'Series' object has no attribute 'columns'


level='year'df.unstack(level='year')被忽略時df沒有一個指數級命名year(或者甚至說,blah):

In [102]: df 
Out[102]: 
      year Day1 Day2 
station_id     
210018  1916  4  7 
210018  1917  3  9 
256700  1916 NaN  8 
256700  1917  6  9 

In [103]: df.unstack(level='blah') 
Out[103]: 
     station_id 
year 210018  1916 
     210018  1917 
     256700  1916 
     256700  1917 
Day1 210018   4 
     210018   3 
     256700   NaN 
     256700   6 
Day2 210018   7 
     210018   9 
     256700   8 
     256700   9 
dtype: float64 

這是令人驚訝的錯誤的來源。

+0

謝謝!我雖然這是一個問題,但我一直讓station_id成爲索引而不是一年。 – spotter 2015-03-31 17:33:57