Search code examples
pythonpandastranspose

Transpose rows to column with limit of special character


Data is in columns which needs to transpose (rows) into column, with limit on special character like '.' fullstop. After each special character like ".", loop repeats until the whole column changes to row. , so in output it is as below.

df :

index ID-0 ID-1 info_string
0 4 20 This
1 6 8 is 
2 8 6 an 
3 12 15 apple
4 29 9.
5 45 5 It
6 56 8 is
7 60 10 sweet
8 62 15 .

output df_out :

index ID-0 ID-1 info_string
0 4 6 8 12 29 20 8 6 15 9 This is an apple.
1 45 56 60 62 5 8 10 15 It is sweet.

also . after this, again how to get the original state back like ...

df_out_reverse:

index ID-0 ID-1 info_string
0 4 20 This
1 6 8 is 
2 8 6 an 
3 12 15 apple
4 29 9.
5 45 5 It
6 56 8 is
7 60 10 sweet
8 62 15 .

I tried with the transpose, but I could not stop to the special character. It transposes complete.


Solution

  • long to wide

    s = df.astype(str).groupby(df['info_string'].iloc[::-1].eq('.').cumsum()).agg(' '.join)
    Out[178]: 
                        ID-0         ID-1         info_string
    info_string                                              
    1            45 56 60 62    5 8 10 15       It is sweet .
    2            4 6 8 12 29  20 8 6 15 9  This is an apple .
    

    wide to long

    pd.concat([s[x].str.split().explode() for x in s.columns],axis=1)
    Out[184]: 
                ID-0 ID-1 info_string
    info_string                      
    1             45    5          It
    1             56    8          is
    1             60   10       sweet
    1             62   15           .
    2              4   20        This
    2              6    8          is
    2              8    6          an
    2             12   15       apple
    2             29    9           .