[Python] csv to dataframe with several delimiters and software bug

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to read in a csv-file. Sadly this is created by a buggy software: My header row contains multiple separators ";" and ",". Which wouldn't be a problem if my dataset wouldn't have a decimal ",".

    I would like to automatically read in the files without changing the header by hand but I did not find something according to in the Pandas documentation.

    The example data:

    some file header row number 1
    some file header row number two
    some more information about data in that file
    column1;column2;column3,column4,column5
    1,234;2,665;0,888;3,891;3,762
    2,232;1,233;0,888;3,789;3,524


    Can I specify a different separator for the header than for the dataset itself? My code:

    df = pd.read_csv(
    "mypath\test.csv",
    skiprows = 3;header= 0, delimiter=';,', decimal = ",")


    which results in reading everything into one column

    column1;column2;column3,column4,column5
    0 1,234;2,665;0,888;3,891;3,762
    1 2,232;1,233;0,888;3,789;3,524


    But the result should be five columns:

    column1 column2 column3 column4 column5
    0 1.234 2.665 0.888 3.891 3.762
    1 2.232 1.233 0.888 3.789 3.524

    Continue reading...

Compartilhe esta Página