How to select and delete columns with duplicate name in pandas DataFrame

前端 未结 4 653
难免孤独
难免孤独 2021-02-07 06:03

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=

4条回答
  •  渐次进展
    2021-02-07 06:26

    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
    

提交回复
热议问题