Search code examples
pythonpandasdataframepivotsplice

Spilt items in a row to columns through pandas


I need to splits items in a row to columns on the occurence of '^' character. This needs to done through pandas without a loop preferably

I have

0   A^
3   206-1C
4   502-2B
5   506-0.5C
6   604-1B
7   907-2B
8   G.ELITE^
9   A201-1C
10  A202-1B

I want

A^ G.ELITE^
206-1C A201-1C.
502-2B. A202-1B
506-0.5C
604-1B
907-2B.
test=rd.query('texts.str.contains("\^")', engine='python')

Solution

  • One way to do this is to split the dataframe where the value ends in ^ and then join those split dataframes horizontally, setting the column names from the splitting values:

    # find values ending with ^
    m = df.iloc[:, 0].str.endswith('^')
    
    # split the dataframe at those points
    s = list(np.nonzero(m)[0]) + [len(df)]
    dfs = [df.iloc[s[i]+1:s[i+1]].reset_index(drop=True) for i in range(len(s)-1)]
    
    # append the split frame as columns
    out = pd.concat(dfs, axis=1).fillna('')
    
    # and set the column names
    out.columns = df[m].iloc[:, 0].to_list()
    

    Assumed sample data:

    df = pd.DataFrame({'value': ['A^', '206-1C', '502-2B', '506-0.5C', '604-1B', '907-2B', 'G.ELITE^', 'A201-1C', 'A202-1B']})
    

    Output:

             A^ G.ELITE^
    0    206-1C  A201-1C
    1    502-2B  A202-1B
    2  506-0.5C
    3    604-1B
    4    907-2B