【问题标题】:Fix: Unalignable boolean Series provided as indexer trying to multiply across columns修复:作为索引器提供的不可对齐的布尔系列试图跨列相乘
【发布时间】:2020-01-03 04:06:17
【问题描述】:

我创建了一个具有一列名称和四列 int64 的数据透视表数据框。

issuer = pos.pivot_table(index="Issuer", columns="AssetType", 
values="MarketValue", aggfunc=np.sum)

我需要找到所有数字组合具有正整数和负整数的所有行。

我尝试了两种方法来解决这个问题。首先是 def product_all 和 itertools.product 在我浏览每一行/组合时的漫长道路。

def product_all(row):
    list_iter = list(row)
     for x, y in itertools.product(list_iter, repeat=2):
        if x*y >= 0:
            return True
        else:
            return False

另一个只是使用 .any() 并寻找值 > 和

issuer = issuer[((issuer[col] > 0).any()) & ((issuer[col] < 0).any())]

这是我的 df 的 head()

AssetType                    Bond  CDS  Equity         Loan
Issuer                                                     
Name1                         0.0 -0.0     0.0   6900238.93
Name2                         0.0 -0.0     0.0  12130000.00
Name3                         0.0 -0.0     0.0   8501753.71
Name4                         0.0 -0.0     0.0  25255509.41
Name5                         0.0 -0.0     0.0  21746971.29

当我同时运行时,我得到了同样的错误: pandas.core.indexing.IndexingError:作为索引器提供的不可对齐的布尔系列(布尔系列的索引和索引对象的索引不匹配

当我打印 x,y 时,我得到了我期望的确切组合。不确定我的代码在哪里破坏。

【问题讨论】:

    标签: python-3.x pandas pivot-table


    【解决方案1】:

    您需要使用anyaxis=1

    试试:

    issuer[issuer.gt(0).any(axis=1) & issuer.lt(0).any(axis=1)]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-20
      • 2018-01-03
      • 2020-12-15
      • 2020-08-05
      • 2023-02-03
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多