Reputation: 13
I have this dataframe where I want to add another column depending on whether change is greater than a certain value, for example 1.03
so that I start with:
index change
2000-02-18 0.995383
2000-02-22 0.956925
2000-02-23 0.984765
2000-02-24 1.033910
2000-02-25 1.030220
2000-02-28 1.025333
and it outputs:
index change label
2000-02-18 0.995383 0
2000-02-22 0.956925 0
2000-02-23 0.984765 0
2000-02-24 1.033910 1
2000-02-25 1.030220 1
2000-02-28 1.025333 0
Upvotes: 0
Views: 62
Reputation: 353359
You can perform a vectorized comparison:
>>> df["label"] = df["change"] > 1.03
>>> df
index change label
0 2000-02-18 0.995383 False
1 2000-02-22 0.956925 False
2 2000-02-23 0.984765 False
3 2000-02-24 1.033910 True
4 2000-02-25 1.030220 True
5 2000-02-28 1.025333 False
or, if you prefer:
>>> df["label"] = (df["change"] > 1.03).astype(int)
>>> df
index change label
0 2000-02-18 0.995383 0
1 2000-02-22 0.956925 0
2 2000-02-23 0.984765 0
3 2000-02-24 1.033910 1
4 2000-02-25 1.030220 1
5 2000-02-28 1.025333 0
(There are lots of other ways to convert from bool
to int
(+0
, *1
, etc.) but this is more explicit.)
Upvotes: 1