gpt4 book ai didi

python - 在 Pandas 中合并数据帧抛出 'AssertionError: cannot create BlockManager._ref_locs'

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:00 24 4
gpt4 key购买 nike

我正在尝试将多个文件从 csv 导入到单个 DataFrame 中,但在尝试添加第三个 DataFrame 时出现以下错误>.

AssertionError: cannot create BlockManager._ref_locs because block [ObjectBlock: [CompletionDate, Categories, DateEntered_x, <lots more columns here>...], dtype=object)] does not have _ref_locs set

代码是:

project = pandas.read_csv(read_csv('dbo_Project.csv')
project = pandas.read_csv(read_csv('dbo_ProjectEnergy.csv')
project = pandas.read_csv(read_csv('dbo_BuildingDescription.csv')
part_merged = pandas.merge(project, project_energy,
on='ProjectID',
how='outer')
part_merged = pandas.merge(part_merged, project_energy_data,
on='ProjectEnergyID',
how='outer')
part_merged = pandas.merge(part_merged, building_description,
on='ProjectEnergyID',
how='outer')

我应该如何加入这些 DataFrames 来避免这个问题?

根据 Stefan Jansen 的回答编辑:

到新错误发生点为止的新代码是:

project = pandas.read_csv(read_csv('dbo_Project.csv')
project = pandas.read_csv(read_csv('dbo_ProjectEnergy.csv')
part_merged = pandas.concat([project, project_energy],
axis=1,
join='outer')
part_merged.set_index(['ProjectEnergyID'])
part_merged = pandas.concat([self.part_merged,
project_energy_data],
axis=1,
join='outer')

最佳答案

一个很好的简单答案。

问题是列重复。导致问题的列并不重要,因此我在合并之前删除了它们。

def remove_clashes(df):
unwanted_cols = ['DataCompleteness', 'DeletedFlag','DateEntered', 'EnteredBy',
'LastModified', 'MandatoryDataInput', 'ModifiedBy']
return df.drop([col for col in unwanted_cols if col in df.columns], axis=1)

关于python - 在 Pandas 中合并数据帧抛出 'AssertionError: cannot create BlockManager._ref_locs',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18173753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com