[Python] Python Pandas: convert list of objects to a list of integer

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    Hi I have a prboblem to convert list of objects to a list of integers. The objects are within the "stopsequence" column of the Pandas data frame "Kanten". All of this I receive after so CSV importing and data cleaning in the column. I am using Python 3.X

    I am a Python newbie, maybe that's part of the problem here.

    import pandas as pd
    import numpy as np
    import os
    import re
    import ast
    orgn_csv = pd.read_csv(r"Placeholder path for csv file")
    df = orgn_csv.dropna()
    Kanten = pd.DataFrame({"stopsequence" : df.stopsequence})

    # In between is a block in which I use regular expressions for data cleaning purposes.
    # I left the data cleaning block out to make the post shorter


    Kanten.stopsequence = Kanten.stopsequence.str.split (',')
    print (Kanten.head())
    print (Kanten.stopsequence.dtype)


    This gives the following output:

    stopsequence
    2 [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
    3 [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
    4 [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
    5 [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
    6 [67, 945, 123, 122, 996, 995, 80, 81, 184, 990...
    object


    I am looking for a way to transform the list which contains objects. I searched through the StackOverflow Forum intensively and tried a bunch of different approaches. With none of them I was succesfull. I tryed to use:

    astype(str).astype(int)

    Kanten.stopsequence = Kanten.stopsequence.astype(str).astype(int)
    This Returns:
    ValueError: invalid literal for int() with base 10:


    adapted the following post with the use of atoi instead of atof

    Kanten.stopsequence.applymap(atoi)
    This Returns:
    AttributeError: 'Series' object has no attribute 'applymap'


    list(map())

    Kanten.stopsequence = list(map(int, Kanten.stopsequence))
    This returns:
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'


    apply(ast.literal_eval)

    Kanten.stopsequence = Kanten.stopsequence.apply(ast.literal_eval)
    This returns:
    TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'


    Does anybody see a solution for that? I am uncertain if it's a complicated case or I just lacke some further programming experience. If possible a short explanation would be helpful. That I can find a solution myself againg. Thank you in advance.

    Continue reading...

Compartilhe esta Página