I have 3
columns of data. I want the entire data to be replaced by zero
, except for the 5-10, 8-11, and 3-12
data points respectively.
Input
-0.0007 -0.0236 0.00579
0.00151 -0.0135 -0.0195
-0.0163 -0.00185 0.00722
0.0207 0.00998 -0.0387
-0.0246 -0.0274 -0.0108
0.0123 -0.0256 0.0137
-0.00963 0.0023 0.0305
-0.0147 0.0255 -0.00806
0.000488 -0.0187 5.29e-05
-0.0167 0.0105 -0.0204
0.00653 0.0176 -0.00643
0.0154 -0.0136 0.00415
-0.0147 -0.00339 0.0175
Expected output
-0.0007 -0.0236 0.00579
0.0015 -0.0135 -0.0195
-0.0163 -0.00185 0.00722
0.0207 0.00998 0.0000
-0.0246 -0.0274 0.0000
0.0000 -0.0256 0.0000
0.0000 0.0023 0.0000
0.0000 0.0255 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
0.0154 0.0000 0.0000
-0.0147 -0.00339 0.0000
script:
import numpy as np
import pandas as pd
data = pd.DataFrame(np.loadtxt("input"),columns=list('ABC'))
data['A'] = data['A'].replace([5:10],'0.0')
data['B'] = data['B'].replace([8:11],'0.0')
data['C'] = data['C'].replace([3:12],'0.0')
error:
SyntaxError: invalid syntax
Please suggest how I can do it in easier way.Thanks.
CodePudding user response:
You're almost there, just use df.loc
will do the trick.
df["A"].loc[5:10] = 0
df["B"].loc[8:11] = 0
df["C"].loc[3:12] = 0
On the other hand, if you want to keep the mentioned value while putting other rows to zero, you can do this.
df["A"] = df["A"].iloc[5:10]
df["B"] = df["B"].iloc[8:11]
df["C"] = df["C"].iloc[3:12]
df.fillna(0,inplace=True)
df
Out[159]:
A B C
0 0.000000 0.000000 0.000000
1 0.000000 0.000000 0.000000
2 0.000000 0.000000 0.000000
3 0.000000 0.000000 0.034555
4 0.000000 0.000000 0.299927
5 0.132431 0.000000 0.869628
6 0.950144 0.000000 0.103173
7 0.274763 0.000000 0.636844
8 0.692817 0.287867 0.009558
9 0.992086 0.963698 0.741168
10 0.000000 0.818806 0.723860
11 0.000000 0.000000 0.831885