developer websup
developer websup

Reputation: 25

Pandas : Joining two dataframes on a common column name and re-ordering the column order

First DataFrame : prac1 and second one : prac_dates DataFrame

After writing the following code it gives the userwarning:

pracs = prac_dates.join(prac1,how='inner',on = "Month")

Joined Dataframe

What I wish to achieve is to
1)Remove the user warning and reorder the column sequence to ('Month','ShipName','ComoQty')and,
2) Rename the (shipname,01),(shipname,02)... column to (shipcount,01),(shipcount,02)...

Upvotes: 0

Views: 94

Answers (1)

Sy Ker
Sy Ker

Reputation: 2190

  1. The user warning comes from the fact that one of your data-frame is multi-index. Either flatten the index or silence the warning;
import warnings

# ignore user warning
warnings.simplefilter(action='ignore', category=UserWarning)
  1. Reordering columns in pandas;
>>> df.columns 
['wrong', 'order', 'columns']
>>> df = df['order', 'wrong', 'columns']
>>> df.columns
['order', 'wrong', 'columns']
  1. Renaming columns;
>>> df.columns = ['new', 'column', 'names']
>>> df.columns
['new', 'column', 'names']

Edit:
To answer your question specifically, I gather that you just want to replace shipname with shipcount;

df.columns = [col.replace('shipname', 'shipcount') for col in df.columns]

Upvotes: 1

Related Questions