1

我正在尝试将多个文件导入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')
4

2 回答 2

7

一个很好的简单答案。

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

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)
于 2013-08-16T16:06:38.613 回答
3

我更喜欢pandas.concat()用于多个帧。也有'outer'选项 -请参阅文档。

如果您要合并的列是索引列,这将很有效,您可以使用 来实现pandas.set_index(),可能在前面加上.reset_index().

于 2013-08-11T16:10:51.350 回答