Home>
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
x | y | z | |
---|---|---|---|
1 | 0.1 | 0.1 | |
2 | -0.1 | 0.1 | |
3 | 0.2 | 0.2 | |
4 | -0.2 | 0.2 | |
5 | 0.4 | 0.4 | |
6 | -0.4 | 0.4 |
Dataframe I want to find
x | y | z | |
---|---|---|---|
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'])
Forz, 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
-
Answer # 2
The average df of the three values is
Calculate withfor 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)
Related articles
- 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 another process after javascript asynchronous process ends
- 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
Trends
Please use
Series.groupby (). transform ()
instead of using a for statementAppend
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 // 3
as 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).If groupby is doubled, you can write with one liner, but readability seems to be