How to select and delete columns with duplicate name in pandas DataFrame
问题 I have a huge DataFrame , where some columns have the same names. When I try to pick a column that exists twice, (eg del df['col name'] or df2=df['col name'] ) I get an error. What can I do? 回答1: You can adress columns by index: >>> df = pd.DataFrame([[1,2],[3,4],[5,6]], columns=['a','a']) >>> df a a 0 1 2 1 3 4 2 5 6 >>> df.iloc[:,0] 0 1 1 3 2 5 Or you can rename columns, like >>> df.columns = ['a','b'] >>> df a b 0 1 2 1 3 4 2 5 6 回答2: Another solution: def remove_dup_columns(frame): keep