如何在Python中有效地检查两列的条件并对第三列执行操作
我有三列和数千行。第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Numpy 提供了 np.where ,它允许进行矢量化测试:
或者如果您想就地更改数组:
Numpy offers
np.where
which allows for vectorized test:or if you want to change the array in place:
您可以为此使用掩码:
输出
my_data
You could use a mask for this:
output in
my_data
如果你想组合一些条件,比如你的代码。您可以在
np.where
& 表示 and 或|
表示 or >:如果您想修改第二列,如 Ballesta 答案
np.ological_and
和np.ological_or
也是可以处理这些情况的其他模块;如果条件超过两个,这些模块必须用作np.ological_and.reduce
和np.ological_or.reduce
。If you want to combine some conditions like your code. you can use operator
&
for and or|
for or innp.where
:and if you want to modify the 2nd column in place, as Ballesta answer
np.logical_and
andnp.logical_or
are the other modules that can handle these such conditions, too; These modules must be used asnp.logical_and.reduce
andnp.logical_or.reduce
if conditions are more than two.也许使用 pandas 更适合这个任务,你可以定义条件并将其应用于表格数据,无需任何显式循环。
Maybe using pandas is more suitable for this task, you can define conditions and apply them to tabular data without any explicit loop.