I have got a dataFrame which looks like this:
index | in | out | time
7 | 8 | 8 | 232
11 | 3 | 0 | 0
79 | 0 | 8 | 12
And I want to create a DataFrame out of this one, where every non-zero in/out value is set to 1 (they are all positive). Time and index should be the same:
index | in | out | time
7 | 1 | 1 | 232
11 | 1 | 0 | 0
79 | 0 | 1 | 12
I think there should be a faster way, than how I am doing this:
df2 = pd.DataFrame({"index":[], "in":[], "out":[], "time":[]})
for index, row in df.iterrows():
if row["in"] == 0:
in_val = 0
else:
in_val = 1
if row["out"] == 0:
out_val = 0
else:
out_val = 1
time = row["time"]
df2 = df2.append(pd.DataFrame({"index":[index], "in":[in_val], "out":[out_val], "time":[time]}), sort=False)
Can I use some lambda function or something like a list comprehension to convert the dataframe faster?
np.whereto change values other than 1 to 1