Pandas - Unable to sort by duplicate column

When I sort one of my data, for example:

my_df.sort(['column_A', 'column_B']) 

I get:

 ValueError: Cannot sort by duplicate column ['A', 'B'] 

Columns have different data and different names. Here is the complete error:

 /Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/frame.pyc in sort(self, columns, column, axis, ascending, inplace) 2534 columns = column 2535 return self.sort_index(by=columns, axis=axis, ascending=ascending, -> 2536 inplace=inplace) 2537 2538 def sort_index(self, axis=0, by=None, ascending=True, inplace=False, /Users/josh/anaconda/envs/py27/lib/python2.7/site-packages/pandas/core/frame.pyc in sort_index(self, axis, by, ascending, inplace, kind) 2603 if k.ndim == 2: 2604 raise ValueError('Cannot sort by duplicate column % s' -> 2605 % str(by)) 2606 indexer = k.argsort(kind=kind) 2607 if isinstance(ascending, (tuple, list)): ValueError: Cannot sort by duplicate column ['A', 'B'] 

Update:

Here is the data frame:

 > my_df.head() db_pixel db_advertiser-campaign 0 Schnucks - Rockford GateHouse Media- Inc. Q1_2013--Katy Pet Cemetary_1.13.14 1 Speedway Auto Mall GateHouse Media- Inc. Q1_2013--Katy Pet Cemetary_1.13.14 2 Hagerstown Honda_Homepage_1.9.14 GateHouse Media- Inc. Q1_2013--Katy Pet Cemetary_1.13.14 3 Mitchell Gold GateHouse Media- Inc. Q1_2013--Katy Pet Cemetary_1.13.14 4 Gambino Realtors - PropelRETARGET GateHouse Media- Inc. Q1_2013--Katy Pet Cemetary_1.13.14 [5 rows x 2 columns] 

Please note that I also have an error with the following command:

 > my_df.head().sort(['db_pixel', 'db_advertiser-campaingn']) 
+7
python pandas
source share
2 answers

I realized that I was calling df.sort(columns=[my_columns]) instead of df.sort(columns=my_columns) . In an effort to simplify the OP, I did not exactly write the exact call I made. Sorry for the confusion

+2
source share

I had the same problem and I was able to fix it by inserting parentheses into square brackets:

 my_df.head().sort([('db_pixel', 'db_advertiser-campaign')]) 
+2
source share

All Articles