Pandas什么时候默认播放系列和数据帧?

问题描述 投票:16回答:2

在尝试回答this question时,我遇到了一些好奇的东西(对我而言)。

假设我想比较一系列形状(10,)和df形状(10,10):

np.random.seed(0)
my_ser = pd.Series(np.random.randint(0, 100, size=10))
my_df = pd.DataFrame(np.random.randint(0, 100, size=100).reshape(10,10))
my_ser > 10 * my_df

正如预期的那样,产生df(10,10)形状的矩阵。比较似乎是划线的。

但请考虑这种情况:

df = pd.DataFrame({'cell1':[0.006209, 0.344955, 0.004521, 0, 0.018931, 0.439725, 0.013195, 0.009045, 0, 0.02614, 0],
              'cell2':[0.048043, 0.001077, 0,0.010393, 0.031546, 0.287264, 0.016732, 0.030291, 0.016236, 0.310639,0], 
              'cell3':[0,0,0.020238, 0, 0.03811, 0.579348, 0.005906, 0,0,0.068352, 0.030165],
              'cell4':[0.016139, 0.009359, 0,0,0.025449, 0.47779, 0, 0.01282, 0.005107, 0.004846, 0],
              'cell5': [0,0,0,0.012075, 0.031668, 0.520258, 0,0,0,2.728218, 0.013418]})
i = 0
df.iloc[:,i].shape
>(11,)
(10 * df.drop(df.columns[i], axis=1)).shape
>(11,4)
(df.iloc[:,i] > (10 * df.drop(df.columns[i], axis=1))).shape
>(11,15)

据我所知,这里Pandas用df广播系列。为什么是这样?

可以通过以下方式获得所需的行为:

(10 * df.drop(df.columns[i], axis=1)).lt(df.iloc[:,i], axis=0).shape
>(11,4)

pd.__version__
'0.24.0'
python pandas numpy-broadcasting
2个回答
7
投票

发生的事情是使用内在数据对齐的大熊猫。 Pandas几乎总是将索引上的数据对齐,无论是行索引还是列标题。这是一个简单的例子:

s1 = pd.Series([1,2,3], index=['a','b','c'])
s2 = pd.Series([2,4,6], index=['a','b','c'])
s1 + s2
#Ouput as expected:
a    3
b    6
c    9
dtype: int64

现在,让我们使用不同的索引运行其他几个示例:

s2 = pd.Series([2,4,6], index=['a','a','c'])
s1 + s2
#Ouput
a    3.0
a    5.0
b    NaN
c    9.0
dtype: float64

笛卡尔积与重复索引一起发生,匹配为NaN + value = NaN。

并且,没有匹配的索引:

s2 = pd.Series([2,4,6], index=['e','f','g'])
s1 + s2
#Output
a   NaN
b   NaN
c   NaN
e   NaN
f   NaN
g   NaN
dtype: float64

因此,在您的第一个示例中,您正在创建具有匹配的默认范围索引的pd.Series和pd.DataFrame,因此比较正在按预期进行。在第二个示例中,您将列标题['cell2','cell3','cell4','cell5']与默认范围索引进行比较,该索引返回所有15列且不匹配所有值将为False,NaN比较返回False。


4
投票

最后,Pandas将每个系列值与列匹配,标题与值索引匹配。第二个示例中的索引是0..10,列名称为cell1..4,因此没有列名匹配,您只需追加新列。这基本上将系列视为一个数据框,索引作为列标题。


如果你使你的系列长于列数,你可以在第一个例子中看到pandas的部分内容:

>>> my_ser = pd.Series(np.random.randint(0, 100, size=20))
>>> my_df
    0   1   2   3   4
0   9  10  27  45  71
1  39  61  85  97  44
2  34  34  88  33   5
3  36   0  75  34  69
4  53  80  62   8  61
5   1  81  35  91  40
6  36  48  25  67  35
7  30  29  33  18  17
8  93  84   2  69  12
9  44  66  91  85  39
>>> my_ser
0     92
1     36
2     25
3     32
4     42
5     14
6     86
7     28
8     20
9     82
10    68
11    22
12    99
13    83
14     7
15    72
16    61
17    13
18     5
19     0
dtype: int64
>>> my_ser>my_df
      0      1      2      3      4      5      6      7      8      9   \
0   True   True  False  False  False  False  False  False  False  False
1   True  False  False  False  False  False  False  False  False  False
2   True   True  False  False   True  False  False  False  False  False
3   True   True  False  False  False  False  False  False  False  False
4   True  False  False   True  False  False  False  False  False  False
5   True  False  False  False   True  False  False  False  False  False
6   True  False  False  False   True  False  False  False  False  False
7   True   True  False   True   True  False  False  False  False  False
8  False  False   True  False   True  False  False  False  False  False
9   True  False  False  False   True  False  False  False  False  False

      10     11     12     13     14     15     16     17     18     19
0  False  False  False  False  False  False  False  False  False  False
1  False  False  False  False  False  False  False  False  False  False
2  False  False  False  False  False  False  False  False  False  False
3  False  False  False  False  False  False  False  False  False  False
4  False  False  False  False  False  False  False  False  False  False
5  False  False  False  False  False  False  False  False  False  False
6  False  False  False  False  False  False  False  False  False  False
7  False  False  False  False  False  False  False  False  False  False
8  False  False  False  False  False  False  False  False  False  False
9  False  False  False  False  False  False  False  False  False  False

注意发生了什么 - 92与第一列进行比较,所以你得到93的单个False。然后将36与第二列等进行比较。如果你的系列长度匹配你的列数,那么你得到预期的行为。

但是当你的系列更长时会发生什么?那么,您需要在数据框中附加一个新的假列以继续比较。它充满了什么?我没有找到任何文档,但我的印象是它只填写False,因为没有什么可比的。因此,你得到额外的列来匹配系列长度,所有False

但你的例子怎么样?你没有得到11列,但4 + 11 = 15!让我们做另一个测试:

>>> my_df = pd.DataFrame(np.random.randint(0, 100, size=100).reshape(10,10),columns=[chr(i) for i in range(10)])
>>> my_ser = pd.Series(np.random.randint(0, 100, size=10))
>>> (my_df>my_ser).shape
(10, 20)

这次我们得到了维度的总和,10 + 10 = 20,作为输出列的数量!

有什么区别? Pandas将每个系列索引与匹配的列标题进行比较。在您的第一个示例中,my_sermy_df标题的索引匹配,因此将它们进行比较。如果有额外的列 - 以上是发生的事情。如果所有列都有不同的名称,那么系列索引,那么所有列都是额外的,你得到你的结果,以及我的例子中标题现在是字符和索引整数的情况。

© www.soinside.com 2019 - 2024. All rights reserved.