If I have a pandas dataframe that looks like this:
X [ m ] Y [ m ] Z [ m ]
0 1 1 0.0
1 2 0.5 0.1
2 3 2 0.3
3 4 1 0.4
4 5 3 0.5
5 1 4 0.6
6 2 1.5 0.8
7 3 6 1.0
8 4 3 1.2
9 5 4 1.5
...
How would I add a fourth column (Z_2) that would look like this (only include values that are a 0.5 jump):
X [ m ] Y [ m ] Z [ m ] Z_2 [ m ]
0 1 1 0.0 0.0
1 2 0.5 0.1 0.0
2 3 2 0.3 0.5
3 4 1 0.4 0.5
4 5 3 0.5 0.5
5 1 4 0.6 0.5
6 2 1.5 0.8 1.0
7 3 6 1.0 1.0
8 4 3 1.2 1.0
9 5 4 1.5 1.5
...
You can try:
df["Z_2"] = (df["Z"] // 0.5) * 0.5
If you're talking about only 0.5 "jump"in the Z column.
You could try something like:
df["Z_2"] = (df["Z"] % .5 == 0).cumsum() * .5
Here the df["Z"] %.5 == 0
finds the values in df["Z"]
that are on the.5 boundary, the cumsum
will only add 1 when a True
is encountered.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.