Solving Python Pandas FutureWarning: Sorting because non-concatenation axis is not aligned.

Solving Python Pandas FutureWarning: Sorting because non-concatenation axis is not aligned happens when you work with DataFrames which have different columns.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

Exploring the Error : Python Pandas FutureWarning: Sorting because non-concatenation axis is not aligned.

This is a common error which happens when you work with DataFrames that have different columns.

The error should look like this. Double check in order to avoid mixing between errors.

                                                                       #
FutureWarning: Sorting because 
non-concatenation axis is not aligned. A future version of pandas will change to not sort by 
default. To accept the future behavior, pass 'sort=True'. To retain the current behavior 
and silence the warning, pass sort=False
                                                                       #

Bellow is the solution which has worked for me and will help you to successfully solve your problem.

Solution : pass the parameter sort=True to silence it and more …

The problem occurs when the DataFrames have different columns, or the columns are in a different order while no parameter sort is set, by default sort=None.

Here is an example with two dataframes dframe1 and dframe2

                                                                       #
dframe1 = pd.DataFrame({"a": [1, 2], "y": [0, 8]}, columns=['y', 'x'])
dframe2 = pd.DataFrame({"a": [4, 5], "y": [7, 3]}, columns=['x', 'y'])
                                                                       #

The error/warning occurs when we try to concatenate the two dataframes.

                                                                       #
print (pd.concat([dframe1, dframe2]))
                                                                       #

If you pass the parameter sort=True you will be able to silence it like in the code bellow.

                                                                       #
placement_by_video_summary = placement_by_video_summary.drop(placement_by_video_summary_new.index)
                                                       .append(placement_by_video_summary_new, sort=True)
                                                       .sort_index()
                                                                       #

I hope this solution fixes your problem.

If this article has been useful for your particular case, consider donating to our Kofi account, there is a red button at the top of this page.

Summing-up

The article is over, I hope I have been able to help you solve this error or at least guide you in the right direction, check out other solutions to different errors you can do that by using the search bar on top of this page.

Keep learning, keep coding guys, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/