[Python] "Cannot set a DataFrame with multiple columns to the single column" when script is in...

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function which processes dataframe of 6 columns. It looks like this:

    def Process_DF():

    DF_6cols = "some data"

    #Two functions to split column containing Column Val1 and Column Val2
    def split_column_1(string):
    str_len = len(string)
    range_obj = range(str_len)
    character = ","
    for index in range_obj:
    if string[index] == character:
    position = index
    return string[0:position]


    def split_column_2(string):
    str_len = len(string)
    range_obj = range(str_len)
    character = ","
    for index in range_obj:
    if string[index] == character:
    position = index
    return string[position+1:]


    #Splitting column 6 into two proper columns and dropping it
    DF_7cols["Column Val1"] = DF_6cols.apply(lambda x: split_column_1(x["Splittable Column"]), axis=1)
    DF_8cols["Column Val2"] = DF_7cols.apply(lambda x: split_column_2(x["Splittable Column"]), axis=1)
    DF_ToReturn = PTT_DataFrame_6Columns.drop("Splittable Colum", axis=1)

    return DF_ToReturn


    When trying to use from subscript import Process_DF and calling it from main script, I'm getting following error:

    File "subscript.py", line x, in Process_DF
    DF_7cols["Column Val1"] = DF_6cols.apply(lambda x: split_column_1(x["Splittable Column"]), axis=1)
    File "~\Python\Python310\site-packages\pandas\core\frame.py", line 4301, in __setitem__
    self._set_item_frame_value(key, value)
    File "~\Python\Python310\site-packages\pandas\core\frame.py", line 4459, in _set_item_frame_value
    raise ValueError(
    ValueError: Cannot set a DataFrame with multiple columns to the single column Column Val1


    On the other hand, when trying to take it out of function and run same script - no error is happening and it is running ok. I also tried to take functions split_column_1 and split_column_2 out as well and declare them in main script or declare them in subscript file and import. Not sure why pd is trying to put multiple columns in one in case of function, when it clearly doesn't outside of it. Python used - 3.10, Pandas - 2.2.3

    I checked other questions about similar problem, but they had solutions which doesn't solve my case.

    For reprex you can use dataframe:

    Index Splittable Column
    1 ,FALSE
    2 Not Acceptable, FALSE
    3 Not Acceptable, FALSE
    4 ,FALSE
    5 ,FALSE
    6 ,FALSE


    Is there a fix for this or way around it?

    Continue reading...

Compartilhe esta Página