{"id":262,"hash":"c3d7e4c9f0cb05606281952657e09f263d5b16be3f6f4ac87580995606de9efb","pattern":"What does `ValueError: cannot reindex from a duplicate axis` mean?","full_message":"I am getting a ValueError: cannot reindex from a duplicate axis when I am trying to set an index to a certain value. I tried to reproduce this with a simple example, but I could not do it.\n\nHere is my session inside of ipdb trace. I have a DataFrame with string index, and integer columns, float values. However when I try to create sum index for sum of all columns I am getting ValueError: cannot reindex from a duplicate axis error. I created a small DataFrame with the same characteristics, but was not able to reproduce the problem, what could I be missing?\n\nI don't really understand what ValueError: cannot reindex from a duplicate axismeans, what does this error message mean? Maybe this will help me diagnose the problem, and this is most answerable part of my question.\n\nipdb> type(affinity_matrix)\n<class 'pandas.core.frame.DataFrame'>\nipdb> affinity_matrix.shape\n(333, 10)\nipdb> affinity_matrix.columns\nInt64Index([9315684, 9315597, 9316591, 9320520, 9321163, 9320615, 9321187, 9319487, 9319467, 9320484], dtype='int64')\nipdb> affinity_matrix.index\nIndex([u'001', u'002', u'003', u'004', u'005', u'008', u'009', u'010', u'011', u'014', u'015', u'016', u'018', u'020', u'021', u'022', u'024', u'025', u'026', u'027', u'028', u'029', u'030', u'032', u'033', u'034', u'035', u'036', u'039', u'040', u'041', u'042', u'043', u'044', u'045', u'047', u'047', u'048', u'050', u'053', u'054', u'055', u'056', u'057', u'058', u'059', u'060', u'061', u'062', u'063', u'065', u'067', u'068', u'069', u'070', u'071', u'072', u'073', u'074', u'075', u'076', u'077', u'078', u'080', u'082', u'083', u'084', u'085', u'086', u'089', u'090', u'091', u'092', u'093', u'094', u'095', u'096', u'097', u'098', u'100', u'101', u'103', u'104', u'105', u'106', u'107', u'108', u'109', u'110', u'111', u'112', u'113', u'114', u'115', u'116', u'117', u'118', u'119', u'121', u'122', ...], dtype='object')\n\nipdb> affinity_matrix.values.dtype\ndtype('float64')\nipdb> 'sums' in affinity_matrix.index\nFalse\n\nHere is the error:\n\nipdb> affinity_matrix.loc['sums'] = affinity_matrix.sum(axis=0)\n*** ValueError: cannot reindex from a duplicate axis\n\nI tried to reproduce this with a simple example, but I failed\n\nIn [32]: import pandas as pd\n\nIn [33]: import numpy as np\n\nIn [34]: a = np.arange(35).reshape(5,7)\n\nIn [35]: df = pd.DataFrame(a, ['x', 'y', 'u', 'z', 'w'], range(10, 17))\n\nIn [36]: df.values.dtype\nOut[36]: dtype('int64')\n\nIn [37]: df.loc['sums'] = df.sum(axis=0)\n\nIn [38]: df\nOut[38]: \n      10  11  12  13  14  15   16\nx      0   1   2   3   4   5    6\ny      7   8   9  10  11  12   13\nu     14  15  16  17  18  19   20\nz     21  22  23  24  25  26   27\nw     28  29  30  31  32  33   34\nsums  70  75  80  85  90  95  100","ecosystem":"pypi","package_name":"pandas","package_version":null,"solution":"This error usually rises when you join / assign to a column when the index has duplicate values. Since you are assigning to a row, I suspect that there is a duplicate value in affinity_matrix.columns, perhaps not shown in your question.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/27236275/what-does-valueerror-cannot-reindex-from-a-duplicate-axis-mean","votes":522,"created_at":"2026-04-19T04:41:41.015668+00:00","updated_at":"2026-04-19T04:51:53.057757+00:00"}