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 - i want to perform the same process for multiple xlsx files in a folder and save them with "_aggregate" at the
- python - iterative optimization (linear programming)
- i want to perform ambiguous character replacement in python
- python 3x - cnn-what is the average pooling process?
- i want to apply iterative processing to the whole sentence in python
- python 3x - about iterative processing when the number of data of python scraping destination is unknown
- python - in django, how to perform a calculation or make an if statement using another value that corresponds to a user-selected
- i get an error when i perform hexadecimal operations in python please tell me the grammar
- python - i want to use k-means to perform cluster division from a state where the cluster is unknown
- python - i want to perform clustering using k-means in a specified scatter plot
- python - about the process of extracting the canceled data based on the absolute value in the data frame
- python - perform a character string search on the dataframe using the for statement and divide it into multiple dataframes
- i want to execute the docker command from a python process
- (python) input () if time elapses without input, skip input () and move to the next process what can i do?
- python - is this code correct in the interpretation of iterative code?
- python iterative processing num is not defend
- python - i want to make a part of the file name a graph title and repeat the process
- 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
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- python 3x - typeerror: 'method' object is not subscriptable
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - unresolved reference comes out in kotlin