Say I have two dataframes.
DF1: col1, col2, col3,
DF2: col2, col4, col5
How do I concatenate the two dataframes horizontally and have the col1, col2, col3, col4, and col5? Right now, I am doing pd.concat([DF1, DF2], axis = 1) but it ends up having two col2's. Assuming all the values inside the two col2 are the same, I want to have only one columns.
To concatenate DataFrames horizontally in Pandas, use the concat(~) method with axis=1 .
By default, when you concatenate two dataframes with duplicate records, Pandas automatically combine them together without removing the duplicate rows.
To drop duplicate columns from pandas DataFrame use df. T. drop_duplicates(). T , this removes all columns that have the same data regardless of column names.
To concatenate DataFrames, use the concat() method, but to ignore duplicates, use the drop_duplicates() method.
Dropping duplicates should work. Because drop_duplicates only works with index, we need to transpose the DF to drop duplicates and transpose it back.
pd.concat([DF1, DF2], axis = 1).T.drop_duplicates().T
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With