如何在Python中有效地检查两列的条件并对第三列执行操作

发布于 2025-01-17 03:42:21 字数 496 浏览 2 评论 0原文

我有三列和数千行。第 1 列和第 2 列中的数字从 1 变为 6。我希望检查第 1 列和第 2 列中的数字组合,以将第 3 列中的值除以某个值。

1     2    3.036010    
1     3    2.622544    
3     1    2.622544    
1     2    3.036010    
2     1    3.036010  

此外,如果交换第 1 列和第 2 列的值,则第 3 列将除以相同的数字。例如,对于1 2 和2 1 组合,列3可以除以相同的值。我目前的方法可以完成这项工作,但我必须手动编写几个条件。执行此任务的更有效方法是什么?提前致谢!

my_data = np.loadtxt('abc.dat')

for row in my_data:    
    if row[0] == 1 and row[1] == 2:
        row[3]/some_value
   



  

I have three columns with thousands of rows. Numbers in column 1 and 2 change from 1 to 6. I desire to check combinations of numbers in both column 1 and 2 to divide the value in column 3 by a certain value.

1     2    3.036010    
1     3    2.622544    
3     1    2.622544    
1     2    3.036010    
2     1    3.036010  

Further, column 3 will be divided by same number if values of column 1 and column 2 are swapped. For example, for 1 2 and 2 1 combinations, column 3 may be divided by same value. My present approach does the job, but I would have to write several conditions manually. What could be more efficient way to perform this task? Thanks in advance!

my_data = np.loadtxt('abc.dat')

for row in my_data:    
    if row[0] == 1 and row[1] == 2:
        row[3]/some_value
   



  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哎呦我呸! 2025-01-24 03:42:21

Numpy 提供了 np.where ,它允许进行矢量化测试:

result = np.where(data[:, 0] == data[:, 1], data[:, 2]/some_value, data[:, 2])

或者如果您想就地更改数组:

data[:, 2] = np.where(data[:, 0] == data[:, 1], data[:, 2]/some_value, data[:, 2])

Numpy offers np.where which allows for vectorized test:

result = np.where(data[:, 0] == data[:, 1], data[:, 2]/some_value, data[:, 2])

or if you want to change the array in place:

data[:, 2] = np.where(data[:, 0] == data[:, 1], data[:, 2]/some_value, data[:, 2])
烟酉 2025-01-24 03:42:21

您可以为此使用掩码:

import numpy as np
my_data = np.column_stack([np.random.randint(1, 6, (1000, 2)), np.random.randn(1000)])
some_value = 123

mask = my_data[:, 0] == my_data[:, 1]
# divide 
my_data[mask, 2] /= some_value

输出 my_data

You could use a mask for this:

import numpy as np
my_data = np.column_stack([np.random.randint(1, 6, (1000, 2)), np.random.randn(1000)])
some_value = 123

mask = my_data[:, 0] == my_data[:, 1]
# divide 
my_data[mask, 2] /= some_value

output in my_data

ぶ宁プ宁ぶ 2025-01-24 03:42:21

如果你想组合一些条件,比如你的代码。您可以在 np.where& 表示 and| 表示 or >:

cond1 = my_data[:, 0] == 1                    # cond is a masked Boolean array for where the first condition is satisfied
cond2 = my_data[:, 1] == 2
some_value = 10
indices = np.where(cond1 & cond2)[0]          # it gets indices for where the two conditions are satisfied
# indices = np.where(cond1 | cond2)[0]        # it gets indices for where at least one of the masks is satisfied
result = my_data[:, 2][indices] / some_value  # operation is done on the specified indices

如果您想修改第二列,如 Ballesta 答案

my_data[:, 2][indices] = my_data[:, 2][indices] / some_value

np.ological_andnp.ological_or 也是可以处理这些情况的其他模块;如果条件超过两个,这些模块必须用作 np.ological_and.reducenp.ological_or.reduce

If you want to combine some conditions like your code. you can use operator & for and or | for or in np.where:

cond1 = my_data[:, 0] == 1                    # cond is a masked Boolean array for where the first condition is satisfied
cond2 = my_data[:, 1] == 2
some_value = 10
indices = np.where(cond1 & cond2)[0]          # it gets indices for where the two conditions are satisfied
# indices = np.where(cond1 | cond2)[0]        # it gets indices for where at least one of the masks is satisfied
result = my_data[:, 2][indices] / some_value  # operation is done on the specified indices

and if you want to modify the 2nd column in place, as Ballesta answer

my_data[:, 2][indices] = my_data[:, 2][indices] / some_value

np.logical_and and np.logical_or are the other modules that can handle these such conditions, too; These modules must be used as np.logical_and.reduce and np.logical_or.reduce if conditions are more than two.

一梦浮鱼 2025-01-24 03:42:21

也许使用 pandas 更适合这个任务,你可以定义条件并将其应用于表格数据,无需任何显式循环。

Maybe using pandas is more suitable for this task, you can define conditions and apply them to tabular data without any explicit loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文