提问人:Dinc Kirikci 提问时间:12/17/2022 最后编辑:phoDinc Kirikci 更新时间:12/18/2022 访问量:145
numpy 数组的元素比较
Element-wise comparison of numpy array
问:
我想问一个关于下表的问题。哪种编码应该可以将每一行与数组中除其它本身之外的其他行进行比较。
例如,我想将第一行与其余列进行比较,以观察第一行的值是否小于该列的其余部分。
例如:
5>2, 8>9,9<5 it is not because 8>9 is not true);
5>4, 8>5,9>11 it is not as well
5>3, 8>7, 9>8 it should be the final answer.
a=[5,8,9]
b=[2,9,5]
c=[4,5,11]
d=[3,7,8]
df = pd.DataFrame({"c1":[5,2,4,3], "c2":[8,9,5,7], "c3":[9,5,11,8]})
# c1 c2 c3
# 0 5 8 9
# 1 2 9 5
# 2 4 5 11
# 3 3 7 8
应该实现哪些 python 代码来获得此特定回报?
我尝试了很多代码块,但从未得到答案,所以如果有人知道如何完成它可以帮助我,我将不胜感激。
答:
0赞
user19077881
12/17/2022
#1
您可以使用一个简单的函数来比较两个项目,无论是单独的列表还是 numpy 数组的行:
def cmp(i, j):
for x, y in zip(i, j):
if x < y:
return False
return True
所以对于你的列表
print(cmp(a, c))
print(cmp(a, d))
0赞
Swifty
12/17/2022
#2
例如,你可以这样做(数学家免责声明:我在非常宽松的意义上使用“不可比”:)):
import numpy as np
a=[5,8,9]
b=[2,9,5]
c=[4,5,11]
d=[3,7,8]
ax = np.array([a, b, c, d])
def compare(l1,l2):
if all([x1>x2 for x1,x2 in zip(l1,l2)]):
return f'{l1} > {l2}'
elif all([x1<x2 for x1,x2 in zip(l1,l2)]):
return f'{l1} < {l2}'
else:
return f'{l1} and {l2} are not comparable'
for i in range(len(ax)):
print([compare(ax[i],ax[j]) for j in range(len(ax)) if j!=i])
输出:
# ['[5 8 9] and [2 9 5] are not comparable', '[5 8 9] and [ 4 5 11] are not comparable', '[5 8 9] > [3 7 8]']
# ['[2 9 5] and [5 8 9] are not comparable', '[2 9 5] and [ 4 5 11] are not comparable', '[2 9 5] and [3 7 8] are not comparable']
# ['[ 4 5 11] and [5 8 9] are not comparable', '[ 4 5 11] and [2 9 5] are not comparable', '[ 4 5 11] and [3 7 8] are not comparable']
# ['[3 7 8] < [5 8 9]', '[3 7 8] and [2 9 5] are not comparable', '[3 7 8] and [ 4 5 11] are not comparable']
0赞
Laurent B.
12/17/2022
#3
第一步:创建具有相同维度但仅填充第一行值的第二个数据帧
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'c1':[5,2,4,3], 'c2':[8,9,5,7], 'c3':[9,5,11,8]})
# df2 : only df1 first row value on definition
df2 = df1[:1]
# Then repeat first row in df2
df2 = df2.loc[np.repeat(df2.index.values, len(df1))]
# reindex like df1
df2.index = df1.index
DF2 :
c1 c2 c3
0 5 8 9
1 5 8 9
2 5 8 9
3 5 8 9
第二步:两个数据帧的区别
# difference between df and df2
df = df1-df2
第三步:查询 df 以获取行号(即 3,因此为 'd')
r = df.query('c1<0 and c2<0 and c3<0')
print(r.index[0])
完整代码:
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'c1':[5,2,4,3], 'c2':[8,9,5,7], 'c3':[9,5,11,8]})
# df2 : only df1 first row value on definition
df2 = df1[:1]
# Then repeat first row in df2
df2 = df2.loc[np.repeat(df2.index.values, len(df1))]
# reindex like df1
df2.index = df1.index
# difference between df1 and df2
df = df1-df2
r = df.query('c1<0 and c2<0 and c3<0')
print(df1.iloc[r.index[0]])
c1 3
c2 7
c3 8
Name: 3, dtype: int64
评论
ax = np.array([a, b, c, d])