Mai*_*aik 5 python replace dataframe pandas
I need to replace values in the dataframe column x. The result should look like x_new. So in detail I have to keep the values in the x column where y is 1 and 255. Between 1 and 255 I must replace the x values with the value where y is 1. The values between 255 and 1 should stay the same. So how can I get the column x_new?
I guess it could work with replace and some condition but I do not know how to combine it. I look forward for any help and hints.
My dataframe looks like e.g.:
x y z x_new
12.28 1 1 12.28
11.99 0 1 12.28
11.50 0 1 12.28
11.20 0 1 12.28
11.01 0 1 12.28
9.74 255 0 9.74
13.80 0 0 13.80
15.2 0 0 15.2
17.8 0 0 17.8
12.1 1 1 12.1
11.9 0 1 12.1
11.7 0 1 12.1
11.2 0 1 12.1
10.3 255 0 10.3
Run Code Online (Sandbox Code Playgroud)
多个步骤但有效。查找 y 为 255 的行的索引,直到找到下一个 1。将值保存在 idx 中。现在使用 idx 和其他两个条件(y == 1 或 y == 255)创建 new_x。把剩下的填满。
# Index of rows between 255 and 1 in column y
idx = df.loc[df['y'].replace(0, np.nan).ffill() == 255, 'y'].index
# Create x_new1 and assign value of x where index is idx or y == 1 or y ==255
df.loc[idx, 'x_new1'] = df['x']
df.loc[(df['y'] == 1) | (df['y'] == 255) , 'x_new1'] = df['x']
# ffill rest of the values in x_new1
df['x_new1'] = df['x_new1'].ffill()
x y z x_new x_new1
0 12.28 1 1 12.28 12.28
1 11.99 0 1 12.28 12.28
2 11.50 0 1 12.28 12.28
3 11.20 0 1 12.28 12.28
4 11.01 0 1 12.28 12.28
5 9.74 255 0 9.74 9.74
6 13.80 0 0 13.80 13.80
7 15.20 0 0 15.20 15.20
8 17.80 0 0 17.80 17.80
9 12.10 1 1 12.10 12.10
10 11.90 0 1 12.10 12.10
11 11.70 0 1 12.10 12.10
12 11.20 0 1 12.10 12.10
13 10.30 255 0 10.30 10.30
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
66 次 |
| 最近记录: |