2013-08-27 177 views
2

說我有數據幀,看起來像這樣滾動重塑蟒蛇大熊貓據幀

In [5]: dates = pd.date_range('20130101',periods=6) 

In [6]: dates 

<class 'pandas.tseries.index.DatetimeIndex'> 
[2013-01-01 00:00:00, ..., 2013-01-06 00:00:00] 
Length: 6, Freq: D, Timezone: None 

In [7]: df = pd.DataFrame(np.arange(0,24).reshape([6,4]),index=dates,columns=list('ABCD')) 

In [8]: df 

      A B C D 
2013-01-01 0 1 2 3 
2013-01-02 4 5 6 7 
2013-01-03 8 9 10 11 
2013-01-04 12 13 14 15 
2013-01-05 16 17 18 19 
2013-01-06 20 21 22 23 

我想重塑df弄成這個樣子

   A B C D A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 
2013-01-03 8 9 10 11 4  5  6  7  0  1  2  3 
2013-01-04 12 13 14 15 8  9  10 11 4  5  6  7 
2013-01-05 16 17 18 19 12 13 14 15 8  9  10 11 
2013-01-06 20 21 22 23 16 17 18 19 12 13 14 15 

基本上,它壓扁之前的兩行把它作爲額外的列。我怎樣纔能有效地實現這一點? (也可我有獨特的列標題太)

+0

這不應該是必要的,你可以使用rolling_apply和朋友做計算* *沒有這個整形黑客。 –

回答

0

我將重複兩次你原來的數據幀,然後在第一個副本(DF1)刪除第一個兩行,在第二(DF2)刪除第一行。然後合併這三個數據框中列順序如下: df1.A .. df1.D df2.A .. df2.D df.A .. df.D

sorrt爲沒有真正的代碼,我是從我的電話寫

2

我真的不知道你爲什麼會這麼做,但在這裏是如何能夠做到:

dates = pd.date_range('20130101',periods=6) 
columns = list('ABCD') 
df = pd.DataFrame(np.arange(0,24).reshape([6,4]),index=dates,columns=columns) 

# First setup some constants 
values = df.values.reshape(df.values.size,) 
step = 4 
size = step * len(columns) 
index = df.index[-step:] 

frame = pd.DataFrame(index=df.index[-step:]) 
for i, pos in enumerate(range(df.values.size-size, -1, -step)): 
    cols = columns if i == 0 else map(lambda x: '%s_%s' % (x, i), columns) 
    new_frame = pd.DataFrame(values[pos:pos+size].reshape((step, len(columns))), 
          index=index, columns=cols) 
    frame = pd.concat([frame, new_frame], axis=1) 
print(frame) 

其中給出:

   A B C D A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 
2013-01-03 8 9 10 11 4 5 6 7 0 1 2 3 
2013-01-04 12 13 14 15 8 9 10 11 4 5 6 7 
2013-01-05 16 17 18 19 12 13 14 15 8 9 10 11 
2013-01-06 20 21 22 23 16 17 18 19 12 13 14 15 
+0

原因用戶可能* *想到他們想要做的,這是自己寫的滾動功能(而不是使用熊貓滾動功能,或'rolling_apply')... __they真的* * shouldn't__(見我的答案]( http://stackoverflow.com/a/18462677/1240268))。 –

+0

@AndyHayden如果是這種情況,我完全同意。但他明確表示他想要**滾動重塑**: - 這就是爲什麼我問他爲什麼。 –

+0

從來沒有理由這樣做。 –

1

大熊貓有wealth of rolling computational functions這意味着你不應該這樣做。這些將大大提高效率(以及更容易推理)。

Function    Description 
rolling_count   Number of non-null observations 
rolling_sum   Sum of values 
rolling_mean   Mean of values 
rolling_median   Arithmetic median of values 
rolling_min   Minimum 
rolling_max   Maximum 
rolling_std   Unbiased standard deviation 
rolling_var   Unbiased variance 
rolling_skew   Unbiased skewness (3rd moment) 
rolling_kurt   Unbiased kurtosis (4th moment) 
rolling_quantile  Sample quantile (value at %) 
rolling_apply   Generic apply 
rolling_cov   Unbiased covariance (binary) 
rolling_corr   Correlation (binary) 
rolling_corr_pairwise Pairwise correlation of DataFrame columns 
rolling_window   Moving window function 

如果您的最終遊戲計劃涉及這些......只是使用這些。如果是別的東西,可以考慮把它寫成通用滾動應用。

舉個例子,這裏有一個rolling_mean與您使用同一個窗口:
即計算在每一行和每一以前兩行完成。

In [11]: df = pd.DataFrame(np.random.randn(24).reshape([6,4]), 
          index=dates,columns=list('ABCD')) 

In [12]: df 
Out[12]: 
        A   B   C   D 
2013-01-01 0.225416 -1.014222 0.724756 -0.594679 
2013-01-02 1.629553 -1.100808 1.279953 -0.058152 
2013-01-03 -0.633830 0.019230 -0.477937 -0.852657 
2013-01-04 -0.601511 0.704212 -1.535412 -1.044537 
2013-01-05 -0.587404 -1.124893 0.834233 0.117244 
2013-01-06 -0.067674 -0.745053 0.589823 -1.007093 

In [13]: pd.rolling_mean(df, 3) 
Out[13]: 
        A   B   C   D 
2013-01-01  NaN  NaN  NaN  NaN 
2013-01-02  NaN  NaN  NaN  NaN 
2013-01-03 0.407046 -0.698600 0.508924 -0.501829 
2013-01-04 0.131404 -0.125788 -0.244465 -0.651782 
2013-01-05 -0.607582 -0.133817 -0.393039 -0.593317 
2013-01-06 -0.418863 -0.388578 -0.037119 -0.644795 

注意:您還可以設置頻率成爲DateOffset(例如天,分鐘,小時等),這將是更難做一個重塑,這給你很多的靈活性。

See the docs更多的例子,以及如何編寫一般的應用。