我正在尝试将多个文件从 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)
我是一名优秀的程序员,十分优秀!