Index order while merging multiple Pandas Series into Dataframe

below is the attached ss-


why is the new_mass arranged alphabetically by index in ascending order after additon of big_mass?
and what can we do to get original order?

Can you also show the output for big_mass as well as you did for mass?

yes here’s the ss-

I have noticed one thing that whenever we apply any operation to the original series , the new series always gets printed in alphatebetical order of its indices whereras the original series still gets printed in the order we defined its index.
Please give some insight to this behaviour and how can we get the new operated series with original order of indices and NOT in ascending order of indices.

Based on running the code, here are my observation.
If the two pd.Series objects to be merged have:

  • exactly same custom indices (for e.g. exactly same set of planets)
  • the custom indices also appear at same location in original Series (for e.g. Mercury at 0 in both series, Venus at 1 on both Series and so on).
    If the above two conditions are met, you will get the same custom index order in resulting pd.Dataframe.

On the other hand, if the merged pd.Series have any difference in custom indexes, for e.g.

  • Both Series have exactly same indices but at different places (for e.g. Mercury at 0 and Venus at 1 in Series 1, but Venus at 0 and Mercury at 1 in 2nd Series)
  • Or, Difference in custom indices
    For e.g. one has more entries, others has less.,
    Some indices are only in one Series.

In such cases, Pandas, while merging, tend to alphabetically arrange the merged custom indexes.

The behavior makes sense, if there are any difference in indices in two Series, it will have to take an approach so that it can uniquely assign indices in the merged Series.

Having said that, my response is based on my observation from running the code and not from reading the documentation.

2 Likes

Alright! makes sense. Thank you so much.