combine dataframes with similar column names

Viewed 29

I have multiple dataframes that have the same metrics data in their columns, except the column names in each dataframe is different. For example:

DataFrame A columns: date, command_83, state_83

DataFrame B columns: date, commands_52, state_52

Dataframe C columns: date, command_67, state_67

Is there a way for me to combine these dataframes together such that the resultant dataframe has columns: date, command, state? And it has added B's data below A, and C's data below B.

1 Answers

You could rename the columns so that all the dataframes will have the same column names, then use pd.concat() with default axis=0 to stack them vertically

dfA.columns = ['date', 'command', 'state']
dfB.columns = ['date', 'command', 'state']
dfC.columns = ['date', 'command', 'state']

df = pd.concat([dfA, dfB, dfC])
Related