Skip to main content
 首页 » 编程设计

python之使用 .loc 的 DataFrame bool 索引

2025年04月02日13zhenyulu

我有一个系列 (business_day),它是通过 isin 生成的,以便使用 dtype: bool:

创建系列
Series: 0 True 
1 True 
2 True 
... 
563 False 
566 False 
Name: Date, dtpye: bool 

然后我尝试使用以下方法更新另一个数组:

myArray.ix[business_day] 

和

myArray.loc[business_day] 

在哪里

myArray: 
DataFrame: Date Epoch Value 
0 2016-05-17 2016-05-17 11:30:00 12345 
1 2016-05-17 2016-05-17 11:31:00 6789.7 
2 2016-05-17 2016-05-17 11:32:00 123.45 

目的是仅修改 myArray,其中的行根据 business_day 系列为“True”。但是上面使用的代码没有效果。我哪里出错了?

请您参考如下方法:

分配结果,因为它不会修改原始 DataFrame:

myarray = myarray.loc[business_day]