我正在尝试将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')
答案 0 :(得分:4)
一个很简单的答案。
问题是重复的列。导致问题的列并不重要,因此我在合并之前将它们删除了。
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)
答案 1 :(得分:3)
我更喜欢将pandas.concat()
用于多个帧。还有'outer'
选项 - see documentation.
如果要合并的列是索引列,这可以很好地工作,您可以使用pandas.set_index()
来实现,可能先于.reset_index()
。