[英]How to add a column in a pandas dataframe with values that repeat?
If I have a pandas dataframe that looks like this:如果我有一个 pandas dataframe 看起来像这样:
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):我将如何添加看起来像这样的第四列 (Z_2)(仅包括 0.5 跳跃的值):
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.如果你在谈论 Z 列中只有 0.5 个“跳跃”。
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.这里
df["Z"] %.5 == 0
找到df["Z"]
中位于 .5 边界上的值, cumsum
只会在遇到True
时加 1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.