我正在尝试将5个数据帧合并为一个数据帧。每个单独的数据框具有相同的格式,唯一的变化是列名称。
# Input Dataframes
df1 = df[['id', 'num', 'type_1', 'object_1', 'notes_1']]
df2 = df[['id', 'num', 'type_2', 'object_2', 'notes_2']]
df3 = df[['id', 'num', 'type_3', 'object_3', 'notes_3']]
df4 = df[['id', 'num', 'type_3', 'object_3', 'notes_3']]
df5 = df[['id', 'num', 'type_3', 'object_3', 'notes_3']]
每次尝试合并它们时,我不小心将它们合并为列而不是行。我的目标是生成具有5行的df
# my attempt
df = pd.concat([df1, df2, df3, df4, df5], axis=0, ignore_index=True)
输出:[type_1, type_2, type_3, type_4, type_5, note_1,notes_2...]
# Desired Output Dataframe
final_df = df[['id', 'num', 'type', 'object', 'notes']]
我不知道如何使用concat()解决这个问题,这有点令人尴尬,因为我想做的正是pandas .concat() documentation中的第一个示例。谁能提供指导?我觉得我快要到了。
答案 0 :(得分:0)
感谢@ scott-boston和@alollz。我认为你们俩都是对的,但我能够按照Scott的建议进行工作。谢谢大家。
# rename columns
d1 = df1a.rename(columns={'id':'id',\
'num':'num',\
'type_1':'type',\
'object_1':'object',\
'notes_1':'notes',}
#concatenate
frames = [d1, d2, d3, d4, d5]
result = pd.concat(frames)