本文介绍了将Pandas DataFrame与Series比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 和,但是他们并没有真正帮助我解决问题.

I've looked at this and this question so far but they didn't really help me with my problem.

问题很简单,但很难说出来.

The problem is very simple but a little difficult to put to words.

我有一个像矩阵这样的数据框:

I have a Dataframe which is matrix like:

       Stock1 Stock2
Date1   3      4
Date2   1      4

对于每个日期(这是我的索引),我想将这些值与系列中的单个点进行比较.

For each date, which is my index, I want to compare the values to a single point in a Series.

成为系列":

      Value
Date1   2
Date2   3

我想通过DataFrame>系列之类的比较来构建以下DataFrame

I want to build the following DataFrame from a comparison like DataFrame > Series

       Stock1 Stock2
Date1   True   True
Date2   False  True

因此,对于Date1,两个值都大于2,而对于Date2,只有Stock2大于3.

So for Date1 both values are greater than 2, and for Date2 only Stock2 is greater than 3.

预先感谢

推荐答案

使用 .gt 并传递axis=0,以与Series进行逐行比较:

Use .gt and pass axis=0 to compare row-wise against a Series:

In [126]:
df.gt(s, axis=0)

Out[126]:
      Stock1 Stock2
index
Date1   True   True
Date2  False   True

这篇关于将Pandas DataFrame与Series比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 14:31