[Python] How to Increase Axis: Pandas: ValueError: Length mismatch: Expected axis has 1...

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    How do I increase my axis to have 30 elements? I am facing this error

    ValueError: Length mismatch: Expected axis has 1 elements, new values have 30 elements


    Listed below is my code

    import pandas as pd
    df = pd.DataFrame()
    for chunk in pd.read_csv('verybig.csv', sep='|', header=None, chunksize=1000, low_memory=False):
    df = pd.concat([df, chunk], ignore_index=True)

    df.columns = ['MyTime','Attribute','Court','Energy','State','30','29','28','27','26','25','24','23','22','21','20','19','18','17','16','15','14','13','12','11','10','9','8','7','6']


    I would like to note that verybig.csv is around 1GB

    UPDATE: After executing df.head(5) I see the following

    >>> df.head(5)
    0
    0 2018-09-01 00:00:03,262, aid:Ut7XsnHXaqf6p4Pyc...
    1 2018-09-01 00:00:16,966, aid:kzhUOAJRHxfgwKdmy...
    2 2018-09-01 00:00:19,008, aid:QeKRGSs4QRP-f8dIZ...
    3 2018-09-01 00:00:19,202, aid:ScGESqQOEY6YkrGoY...
    4 2018-09-01 00:00:24,799, aid:Hajk1NF0tIIVELz2N...
    >>>


    UPDATE 2: I think the path way to my solution is to split the above column (Column 0) using the comma as the separator after the following code is executed

    import pandas as pd
    df = pd.DataFrame()
    for chunk in pd.read_csv('verybig.csv', sep='|', header=None, chunksize=1000, low_memory=False):
    df = pd.concat([df, chunk], ignore_index=True)

    Continue reading...

Compartilhe esta Página