Python 基于条件的熊猫数据提取

Python 基于条件的熊猫数据提取,python,pandas,loops,indexing,Python,Pandas,Loops,Indexing,我有一个数据帧a=[1,2,3,5,9,8,11,13]和B=[2,1,6,19,16,15,14,12]。我想检查的是A和B的交叉元素在任何情况下是否相等 对于例如:这里A[0]==B[1]和B[0]==A[1],这是一个十字交叉的元素 import pandas as pd df=pd.DataFrame({'A':[1,2,3],'B':[2,1,6]}) if df.loc[0,"A"] == df.loc[1,"B"] & df.loc[1,"A"] == df.loc[0,"

我有一个数据帧
a=[1,2,3,5,9,8,11,13]和B=[2,1,6,19,16,15,14,12]
。我想检查的是A和B的交叉元素在任何情况下是否相等

对于例如:这里
A[0]==B[1]和B[0]==A[1]
,这是一个十字交叉的元素

import pandas as pd
df=pd.DataFrame({'A':[1,2,3],'B':[2,1,6]})
if df.loc[0,"A"] == df.loc[1,"B"] & df.loc[1,"A"] == df.loc[0,"B"]:
    print("the values which are equal")
else:
    print("the values which are not equal")
比较连续列 为了检查数据帧中所有行的wheter(是否)
A[i]==B[i+1]&A[i+1]==B[i]
,您可以对列进行矢量比较,但移动:

A = np.array([1,2,3,5,14,16,16,13]) # I mdified input data from the question for the second example
B = [2,1,6,19,16,15,14,12]
df = pd.DataFrame({'A':A,'B':B})
eq_diag = (df['A'][:-1].values==df['B'][1:].values) & (df['A'][1:].values==df['B'][:-1].values)
# boolean array with rows=rows_in_df-1, eq_diag[i] will be true if the 
# diagonal between rows i and i+1 is equal
# Output for eq_diag
# [ True False False False False False False]
然后,可以打印这些对角线比较中相等的值:

print df[:-1][eq_diag] # the [:-1] is important for dimensions to match
# Out
    A  B
 0  1  2 # it also returns the index i (not i+1) where the diagonal is equal
for i,j in combinations[eq_diag]:
    print 'The criss cross element of columns {} and {} is equal:\n{}'.format(i,j,df.values[[i,j]])
# Out
# The criss cross element of columns 0 and 1 is equal:
# [[1 2]
#  [2 1]]
# The criss cross element of columns 4 and 6 is equal:
# [[14 16]
#  [16 14]]
比较数据框中的所有列组合 如果不将列
i
与列
i+1
进行比较,而是应比较所有可能的组合,则可以使用模块
itertools

import itertools
combinations =  np.array(list(itertools.combinations(range(len(df)),2)))
print combinations.T
eq_diag = ((df['A'].values[combinations[:,0]]==df['B'].values[combinations[:,1]]) & 
           (df['A'].values[combinations[:,1]]==df['B'].values[combinations[:,0]]))
# Out: all the column combinations
[[0 0 0 0 0 0 0 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 4 4 5 5 6]
 [1 2 3 4 5 6 7 2 3 4 5 6 7 3 4 5 6 7 4 5 6 7 5 6 7 6 7 7]]
然后,可以打印相等的元素:

print df[:-1][eq_diag] # the [:-1] is important for dimensions to match
# Out
    A  B
 0  1  2 # it also returns the index i (not i+1) where the diagonal is equal
for i,j in combinations[eq_diag]:
    print 'The criss cross element of columns {} and {} is equal:\n{}'.format(i,j,df.values[[i,j]])
# Out
# The criss cross element of columns 0 and 1 is equal:
# [[1 2]
#  [2 1]]
# The criss cross element of columns 4 and 6 is equal:
# [[14 16]
#  [16 14]]

你只想检查一个条件?那你为什么要循环使用
i
j
?你可能想使用
loc
,例如在你的代码中试试:
如果df.loc[0,“A”]==df.loc[1,“B”]和df.loc[1,“A”]==df.loc[0,“B”:
我想检查一下数据帧中的所有元素(即跨越A和B)我不清楚你的意思。还有什么其他因素?你能问你的问题并指定你想做的所有比较吗?你想检查第0行和第1行的对角线,然后检查1和2等吗?@ AMIT我更新了答案,也考虑了所有可能的列组合的情况。