IndexingError:在Pandas中作为索引器提供的不可对齐的布尔序列

2024-09-28 21:55:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我用glob对几个.csv文件进行了排序,然后将它们连接起来,现在我正试图在一个数据框中筛选特定的列(“合同”)

import pandas as pd
from glob import glob

gas_prices_2019= sorted(glob('Gas prices/2019****_PowernextGasFutures_SettlementPrices.csv'))
gas_prices_2019

df_2019= pd.concat((pd.read_csv(file).assign(filename = file)
           for file in gas_prices_2019), ignore_index = True)    

df_2019.loc[df['Contracts'] == 'CEGH VTP 2020' ]

但是,我在代码的最后一行收到以下错误消息:

IndexingError:作为索引器提供的不可对齐的布尔序列(布尔序列的索引和索引对象的索引不匹配)

我研究发现,当.loc命令丢失时会出现此错误消息,但它在我的代码中。 我想筛选到行,其中列“Contracts”中的值是“CEGH VTP 2020”

有人能帮忙吗

多谢各位。致意


Tags: csv代码import消息df错误locglob