0
0 комментариев

Python (3.6) , windows

надеюсь найдутся знатоки которые смогут помочь)

Имеется такая таблица

Код:

<code><span class="pln">X2          X3  X4  Y   Y1
</span><span class="lit">01.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">1</span>   <span class="lit">1</span>   
<span class="lit">02.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">2</span>   <span class="lit">0</span>   
<span class="lit">02.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">3</span>   <span class="lit">0</span>   
<span class="lit">02.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">1</span>   <span class="lit">1</span>   
<span class="lit">03.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">2</span>   <span class="lit">1</span>   
<span class="lit">04.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">3</span>   <span class="lit">0</span>   
<span class="lit">05.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">1</span>   <span class="lit">1</span>   
<span class="lit">06.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">2</span>   <span class="lit">0</span>   
<span class="lit">07.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">3</span>   <span class="lit">1</span>   
<span class="lit">08.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">1</span>   <span class="lit">1</span>   
<span class="lit">09.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">2</span>   <span class="lit">0</span>   
<span class="lit">10.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">3</span>   <span class="lit">1</span>   
<span class="lit">11.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">1</span>   <span class="lit">0</span>   
<span class="lit">12.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">2</span>   <span class="lit">1</span>   
<span class="lit">13.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">3</span>   <span class="lit">0</span>   
<span class="lit">14.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">1</span>   <span class="lit">1</span>   
<span class="lit">15.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">2</span>   <span class="lit">1</span>   
<span class="lit">16.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">3</span>   <span class="lit">0</span>   
<span class="lit">17.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">1</span>   <span class="lit">1</span>   <span class="lit">1</span>   
<span class="lit">18.02</span><span class="pun">.</span><span class="lit">2019</span>  <span class="lit">2</span>   <span class="lit">2</span>   <span class="lit">0</span></code>

И в столбце Y1 необходимо посчитать скользящее среднее столбца Y за последние 5 дней, но только с фильтрацией по условию Х3 и Х4. Фильтр равен текущему значению столбцов для текущей строки. Например для строки 04.02.2019 2 3 0 среднее будет равно 0, потому что для нее условию соответствует только строка 02.02.2019 2 3 0

Как это сделать не понимаю, знаю что это будет нечто вроде

<code><span class="pln">filtered_X4 </span><span class="pun">=</span><span class="pln"> df</span><span class="pun">[</span><span class="str">'X4'</span><span class="pun">].</span><span class="pln">where</span><span class="pun">(</span><span class="pln">condition_1 </span><span class="pun">&</span><span class="pln"> condition_2 </span><span class="pun">&</span><span class="pln"> condition_3</span><span class="pun">)</span></code>

Но как задать сами условия condition_1,2,3 не понимаю.

Видел много примеров когда известен фильтр, например

<code><span class="pln">condition_1 </span><span class="pun">=</span><span class="pln"> df</span><span class="pun">[</span><span class="str">'X2'</span><span class="pun">].</span><span class="pln">isin</span><span class="pun">([</span><span class="lit">2</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">,</span> <span class="lit">5</span><span class="pun">])</span></code>

но это не то что мне нужно. В принципе нужно что-то очень похожее на функцию СРЗНАЧЕСЛИМН в экселе)

Как посчитать среднее знаю

<code><span class="pln">df</span><span class="pun">[</span><span class="str">'Y1'</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> filtered_X4</span><span class="pun">.</span><span class="pln">shift</span><span class="pun">(</span><span class="lit">1</span><span class="pun">).</span><span class="pln">rolling</span><span class="pun">(</span><span class="pln">window </span><span class="pun">=</span> <span class="lit">999999</span><span class="pun">,</span><span class="pln"> min_periods </span><span class="pun">=</span> <span class="lit">1</span><span class="pun">).</span><span class="pln">mean</span><span class="pun">()</span></code>

, но не могу настроить фильтрацию

Asked question
Добавить комментарий