当我对我的一个数据帧进行排序时,例如:
my_df.sort(['column_A', 'column_B'])
我明白了:
ValueError: Cannot sort by duplicate column ['A', 'B']
列具有不同的数据和不同的名称。这是完整的错误:
/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']
以下是数据框:
> my_df.head()
db_pixel db_advertiser-campaign
0 Schnucks - Rockford GateHouse Media- Inc. Q1_2013--Katy's Pet Cemetary_1.13.14
1 Speedway Auto Mall GateHouse Media- Inc. Q1_2013--Katy's Pet Cemetary_1.13.14
2 Hagerstown Honda_Homepage_1.9.14 GateHouse Media- Inc. Q1_2013--Katy's Pet Cemetary_1.13.14
3 Mitchell Gold GateHouse Media- Inc. Q1_2013--Katy's Pet Cemetary_1.13.14
4 Gambino Realtors - PropelRETARGET GateHouse Media- Inc. Q1_2013--Katy's Pet Cemetary_1.13.14
[5 rows x 2 columns]
请注意,我也遇到以下命令的错误:
> my_df.head().sort(['db_pixel', 'db_advertiser-campaingn'])
答案 0 :(得分:2)
我意识到我正在呼叫df.sort(columns=[my_columns])
而不是df.sort(columns=my_columns)
。为了简化OP,我没有准确地写出我正在制作的确切电话。对不起困惑
答案 1 :(得分:2)
我遇到了同样的问题,我可以通过在方括号内插入括号来修复它:
my_df.head().sort([('db_pixel', 'db_advertiser-campaign')])