Search code examples
pythonpandasdataframesplitcell

Multiline values in a cell to single line separated_python


I have a df where values from the first cell of column 'col2' are not in a single line but in multilines as per the following example:

col1   col2
ID1    value1
       value2
       value3
ID2    value4, value5, value6

I want to bring them in a single line as in the example for ID2, single line with any separator between them. Is there a way to do that with python?

my df:

enter image description here

df = pd.DataFrame({'col1': [1, 2], 'col2': ['value1\nvalue2\nvalue3', 'value4, value5, value6']})

Solution

  • try:

    df['col2']=df['col2'].replace('\n',',',regex=True)
    

    OR

    If you wanted to replace all occurances of '\n' to ',' then use:

    df=df.replace('\n',',',regex=True)
    

    output of df:

        col1    col2
    0   1       value1,value2,value3
    1   2       value4, value5, value6