Saya memiliki kerangka data df yang terlihat seperti ini,

   a       b 
0  30.05  29.55
1  30.20  26.05
2  30.81  25.65
3  31.12  26.44
.. ...    ...
85 30.84  25.65
86 31.12  26.44
87 29.55  25.57
88 32.41  25.45
89 21.55  29.57
90 32.91  26.41
91 34.12  25.69

Saya perlu membuat Kolom 'c' baru yang menyimpan nilai Array Kolom 'b' ditambah nilai 4 baris sebelumnya dari Kolom 'b'. Jadi df yang dihasilkan akan terlihat seperti,

     a      b     c
0  30.05  29.55 [29.55,0,0,0,0]
1  30.20  26.05 [26.05,29.55,0,0,0]
2  30.81  25.65 [25.65,26.05,29.55,0,0]
3  31.12  26.44 [26.44,25.65,26.05,29.55,0]
.. ...    ...
85 30.84  25.65 [25.65, 44.60, 30.15, 29.55, 24.66 ]
86 31.12  26.44 [26.44, 25.65, 25.65, 25.65, 25.65 ]
87 29.55  25.57 [25.57, 26.44, 25.65, 25.65, 25.65 ]
88 32.41  25.45 [25.45, 25.57, 26.44, 25.65, 25.65 ]
89 21.55  29.57 [29.57, 25.45, 25.57, 26.44, 25.65 ]
90 32.91  26.41 [26.41, 29.57, 25.45, 25.57, 26.44 ]
91 34.12  25.69 [25.69, 26.41, 29.57, 25.45, 25.57 ]

Saya tahu saya dapat mengakses baris sebelumnya dengan df.b.shift(1) dan df.b.shift(2) dll, tetapi saya ingin dapat dengan mudah mengubah berapa banyak baris yang saya lihat kembali untuk membentuk array dengan variabel daripada ketik banyak shift (n)

Setelah mencari sepanjang hari aku terjebak. (python3.6)

0
Trembles 15 Agustus 2017, 11:42

2 jawaban

Jawaban Terbaik

Anda dapat menggunakan pd.concat dengan range(N)

In [60]: df['c'] = pd.concat([df.b.shift(i) for i in range(4)], 1).fillna(0).values.tolist()

In [61]: df
Out[61]:
        a      b                             c
0   30.05  29.55        [29.55, 0.0, 0.0, 0.0]
1   30.20  26.05      [26.05, 29.55, 0.0, 0.0]
2   30.81  25.65    [25.65, 26.05, 29.55, 0.0]
3   31.12  26.44  [26.44, 25.65, 26.05, 29.55]
85  30.84  25.65  [25.65, 26.44, 25.65, 26.05]
86  31.12  26.44  [26.44, 25.65, 26.44, 25.65]
87  29.55  25.57  [25.57, 26.44, 25.65, 26.44]
88  32.41  25.45  [25.45, 25.57, 26.44, 25.65]
89  21.55  29.57  [29.57, 25.45, 25.57, 26.44]
90  32.91  26.41  [26.41, 29.57, 25.45, 25.57]
91  34.12  25.69  [25.69, 26.41, 29.57, 25.45]

Atau, gunakan np.column_stack pada shift(n)

In [70]: np.column_stack([df.b.shift(i).fillna(0) for i in range(4)]).tolist()
Out[70]:
[[29.55, 0.0, 0.0, 0.0],
 [26.05, 29.55, 0.0, 0.0],
 [25.65, 26.05, 29.55, 0.0],
 [26.44, 25.65, 26.05, 29.55],
 [25.65, 26.44, 25.65, 26.05],
 [26.44, 25.65, 26.44, 25.65],
 [25.57, 26.44, 25.65, 26.44],
 [25.45, 25.57, 26.44, 25.65],
 [29.57, 25.45, 25.57, 26.44],
 [26.41, 29.57, 25.45, 25.57],
 [25.69, 26.41, 29.57, 25.45]]
1
Zero 15 Agustus 2017, 08:53

Anda dapat menggunakan pemahaman daftar bersyarat (untuk memeriksa kapan tampilan balik sebelum nilai pertama dalam indeks).

rows_lookback = 5

df = df.assign(c=[[df['b'].iat[n - i] if n - i >= 0 else 0 
                   for i in range(rows_lookback)] 
                  for n in range(len(df['b']))])
>>> df
        a      b                                    c
0   30.05  29.55                  [29.55, 0, 0, 0, 0]
1   30.20  26.05              [26.05, 29.55, 0, 0, 0]
2   30.81  25.65          [25.65, 26.05, 29.55, 0, 0]
3   31.12  26.44      [26.44, 25.65, 26.05, 29.55, 0]
85  30.84  25.65  [25.65, 26.44, 25.65, 26.05, 29.55]
86  31.12  26.44  [26.44, 25.65, 26.44, 25.65, 26.05]
87  29.55  25.57  [25.57, 26.44, 25.65, 26.44, 25.65]
88  32.41  25.45  [25.45, 25.57, 26.44, 25.65, 26.44]
89  21.55  29.57  [29.57, 25.45, 25.57, 26.44, 25.65]
90  32.91  26.41  [26.41, 29.57, 25.45, 25.57, 26.44]
91  34.12  25.69  [25.69, 26.41, 29.57, 25.45, 25.57]
0
Alexander 15 Agustus 2017, 09:02