I want to write an iterative process for substituting the average value of the three values of the y column in the z column in order for the DataFrame as shown below.
I would appreciate your guidance.
Currently available Dataframe
Dataframe I want to find
|1||0.1||0.1||0.2 (average of y1 to y3)|
|2||-0.1||0.1||0.2 (average of y1 to y3)|
|3||0.4||0.4||0.2 (average of y1 to y3)|
|4||-0.2||0.2||0.3 (average of y4 to y6)|
|5||0.4||0.4||0.3 (average of y4 to y6)|
|6||-0.3||0.3||0.3 (average of y4 to y6)|
The data of x and y is substituted as follows.
import pandas as pd import numpy as np raw = pd.read_csv ('Users/~~~~~/raw.csv', header = 0) ds = pd.DataFrame (index = ,) ds ['x'] = raw ['x'] ds ['y'] = np.fabs (ds ['x'])
z, I tried to write the following code to get three values at a time, but this is a situation where only y2 and y3 data can be obtained.
for i in range (3): df = ds ['y'] [i: i * 2]
Thanks for your guidance.
Answer # 1
Series.groupby (). transform ()instead of using a for statement
df ['z'] = df.groupby (df.index // 3) ['y']. transform ('mean')
For 20 data, divide into the first 10 and the latter 10
If you do it normally, would you divide the first half and the second half of the data into two times (in a loop) and then add the 'z' column by combining the results?
However, if grouping with the value of
index // 3as it is, the latter half of the group will be broken, so the value from 0 to 9 is simply divided by 3 ( (Only integer part).
d =  for i in [0,10]: d.append (df [i: i + 10] .groupby (np.arange (10) // 3) ['y']. transform ('mean')) df ['z'] = pd.concat (d)
If groupby is doubled, you can write with one liner, but readability seems to be
df ['z'] = df.groupby (df.index // 10, group_keys = False) .apply (lambda d: d.groupby (np.arange (10 ) // 3) ['y']. Transform ('mean'))
Answer # 2
The average df of the three values is
for i in range (0, 6, 3): df = sum (ds [i: i + 3])/3
. Then, when substituting for z
for i in range (0, 6, 3): df = sum (ds [i: i + 3])/3 ds ["z"] [i: i + 3] = np.tile (df, 3)
- python 3x - i want to be able to perform the same process at the same time every day
- python api i don't know how to process the returned numbers
- python 3x - i want to divide the process when it is err as a result of command execution in python3 subprocess
- python - how to display an image, make a selection, and process by selection
- python 3x - i want to perform the same processing for each folder under the specified folder
- python - i want to convert numeric data to time and process it
- i want to start a process that is completely independent of python
- in the python if statement, the process is not executed even though the condition is satisfied
- python - i don't know how to process blocking processes in parallel
- i want to wait until the next process can be performed after executing the process in python
- i want to perform multiple processes with else in python list comprehension notation
- python iterative processing num is not defend
- python about iterative processing with specified numbers
- (python) input () if time elapses without input, skip input () and move to the next process what can i do?
- python - when using the cv2adaptivethreshold function in the binarization process of an image, an error occurs in medianblur and
- i want to start the next process when the first process starts in python
- when i execute wikiextractor, "python" is output and the process ends
- python - i get the error shapes (1,1) and (2,) not aligned in the process of leave one out cross variation (loocv)
- python - process without using the same formula twice