pandas 如何比较两个熊猫系列的两排?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39928738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:10:12  来源:igfitidea点击:

How to compare two rows of two pandas series?

pythonpandas

提问by Jiaqi

I have two python pandas series df10and df5. I want to compare their values.For example: df10[-1:]< df5[-1:],it returns true. df10[-2:-1] > df5[-2:-1], it returns false.

我有两个 python Pandas系列df10df5. 我想比较它们的值。例如:df10[-1:]< df5[-1:],它返回true。df10[-2:-1] > df5[-2:-1],它返回假。

But if I combine them together, df10[-1:]< df5[-1:] and df10[-2:-1]>df5[-2:-1],it returns

但是如果我将它们组合在一起df10[-1:]< df5[-1:] and df10[-2:-1]>df5[-2:-1],它会返回

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()

系列的真值是不明确的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

But I expect it returns false. How can I solve this problem?

但我希望它返回 false。我怎么解决这个问题?

采纳答案by rofls

You can do this with the pandas Series valuesattribute:

您可以使用 pandas Seriesvalues属性执行此操作:

if (df10.values[-2:-1] > df5.values[-2:-1]) and\ 
        (df10.values[-1:] < df5.values[-1:]):
    print("we met the conditions!")

回答by blackeneth

Consider you have the two dataframes from this program:

假设您有来自该程序的两个数据框:

# Python 3.5.2
import pandas as pd
import numpy as np

# column names for example  dataframe
cats = ['A', 'B', 'C', 'D', 'E']

df5 = pd.DataFrame(data = np.arange(25).reshape(5, 5), columns=cats)
print("Dataframe 5\n",df5,"\n")

df10=pd.DataFrame(data = np.transpose(np.arange(25).reshape(5, 5)), columns=cats)
print("Dataframe 10\n",df10)

The resulting data frames are:

生成的数据帧是:

Dataframe 5
     A   B   C   D   E
0   0   1   2   3   4
1   5   6   7   8   9
2  10  11  12  13  14
3  15  16  17  18  19
4  20  21  22  23  24 

Dataframe 10
    A  B   C   D   E
0  0  5  10  15  20
1  1  6  11  16  21
2  2  7  12  17  22
3  3  8  13  18  23
4  4  9  14  19  24

Now let's look at the result of your first comparison:

现在让我们看看第一次比较的结果:

print(df5[-1:])
print(df10[-1:])

a=df10[-1:]< df5[-1:]

print("\n",a,"\n",type(a))

which results in:

这导致:

    A   B   C   D   E
4  20  21  22  23  24
   A  B   C   D   E
4  4  9  14  19  24

       A     B     C     D      E
4  True  True  True  True  False 
 <class 'pandas.core.frame.DataFrame'>

Now the second comparison:

现在第二个比较:

print(df5[-2:-1])
print(df10[-2:-1])

b=df10[-2:-1]>df5[-2:-1]
print("\n",b,"\n",type(b))

which has results:

结果如下:

    A   B   C   D   E
3  15  16  17  18  19
   A  B   C   D   E
3  3  8  13  18  23

        A      B      C      D     E
3  False  False  False  False  True 
 <class 'pandas.core.frame.DataFrame'>

The issue:

问题:

If we evaluate:

如果我们评估:

pd.Series([True, True, False, False]) and pd.Series([False, True, False, True])

What is the correct answer?:

正确答案是什么?:

  1. pd.Series([False, True, False, False])
  2. False
  3. True
  4. All of the above
  5. Any of the above
  6. It depends
  1. pd.Series([False, True, False, False])
  2. False
  3. True
  4. 上述所有的
  5. 以上任何一项
  6. 这取决于

The answer is: 6 - It depends. It depends on what you want.

答案是:6 - 这取决于。这取决于你想要什么。

First, we have to create boolean series for the comparison:

首先,我们必须为比较创建布尔系列:

a_new = (df10[-1:] < df5[-1:]).any()
print(a_new,"\n",type(a_new))

b_new = (df10[-2:-1] > df5[-2:-1]).any()
print("\n",b_new,"\n",type(b_new))

The results are:

结果是:

A     True
B     True
C     True
D     True
E    False
dtype: bool 
 <class 'pandas.core.series.Series'>

A    False
B    False
C    False
D    False
E     True
dtype: bool 
 <class 'pandas.core.series.Series'>

Now, we can compute 3 cases.

现在,我们可以计算 3 个案例。

Case 1: a.any() and b.any()

案例 1:a.any() 和 b.any()

a.any() = True if any item in a is True
b.any() = True if any item in b is True
print(a_new.any() and b_new.any())

The result is True.

结果为真。

Case 2: a.all() and b.all()

情况 2:a.all() 和 b.all()

a.all() = True if every item in a is True   
b.all() = True if every item in b is True
print(a_new.all() and b_new.all())

The result is False.

结果是假的。

Case 3: Pairwise comparison

案例 3:成对比较

For this, you have to compare every element to each other.

为此,您必须将每个元素相互比较。

result_pairwise = [a_new and b_new for a_new, b_new in zip(a_new,b_new)]
print(result_pairwise,"\n",type(result_pairwise))

The result is:

结果是:

[False, False, False, False, False] 
 <class 'list'>

For more details:

更多细节: