[Python] pandas processing duplicate column names

Discussão em 'Python' iniciado por Stack, Julho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I have this simple table

    | **Test details** | **Meaning** | **Value** | **Value** |
    | :--------------: | :---------: | :--------------: | :---------------------: |
    | Requirements | | ================ | ======================= |
    | Description | | Nominal Case 1 | Nominal Case 2 |


    Using this command:

    df = pandas.read_csv(
    StringIO(table_str),
    sep='|',
    escapechar='\\',
    skipinitialspace=True
    )


    My expectation was that pandas.read_csv would add the index to the column headers automatically. However, it just prints a warning UserWarning: DataFrame columns are not unique, some columns will be omitted. and doesn't do it.

    But if the columns are the same width - it works fine.

    | **Test details** | **Meaning** | **Value** | **Value** |
    | :--------------: | :---------: | :--------------: | :--------------: |
    | Requirements | | ================ | ================ |
    | Description | | Nominal Case 1 | Nominal Case 2 |


    Anyone knows how to resolve this problem with options to read_csv? I can technically go through df.columns.duplicated() and add the index manually. But the automatic method would be better.

    thank you.

    Continue reading...

Compartilhe esta Página