This is my dataframe:
d = {'id': [1,1,2,2,3,3,3] ,
'a_code': ['abc', 'abclm', 'pqr', 'pqren', 'lmn', 'lmnre', 'xyznt'],
'a_type':['CP','CO','CP','CO','CP','CP','CO'],
'z_code': ['abclm', 'wedvg', 'pqren', 'unfdc', 'lmnre','wqrtn','hgbvcx'],
'z_type': ['CO', 'CO', 'CO','CO','CP','CO','RT'],
'stepNo': [1,2,1,2,1,2,3]
}
df= pd.DataFrame(d)
Each id has rows which are continuous paths defined by stepNo
. I want to print all steps in a single row so I can visualize the path. The stepNo
varies between 2 to 24, so in somecases I could have 5x24 columns. Is it possible to do this?
Output:
id stepNo a_code a_type z_code z_type stepNo a_code a_type z_code z_type stepNo a_code a_type z_code z_type
1 1 abc CP abclm CO 2 abclm CO wedvg CO
2 1 pqr CP pqren CO 2 pqren CO unfdc CO
3 1 lmn CP lmnre CP 2 lmnre CP wqrtn CO 3 xyznt CO hgbvcx RT
UPDATE:
@NYC Coder solution fails for this sample, I would appreciate if someone could help me figure it out, All other answer timeout or arent legible for how the output is needed due to my dataframe having high number of dimensions.
nan = ""
d = {'NAME': [1,1,2,2,3,3,3,3,3,4,4,4,4,4,4],
'col1': ['P100','P100','P100','P100','MS','MS','MS','MS','MS','MS','MS','MS','MS','MS','MS'],
'col2': ['CNMZ',
'CNMZ',
'COMX',
'COMX',
'_NCTE',
'_NCTE',
'_NCTE',
'_NCTE',
'_NCTE',
'T1MF',
'T1MF',
'T1MF',
'T1MF',
'T1MF',
'T1MF'],
'stepNo': [1, 2, 1, 2, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6],
'col4': ['xyz',
'abc',
'pqr',
'gvt',
'mno',
'tru',
'ercm',
'lotr',
'ddlj',
'refv',
'ecv',
'ecv',
'ecv',
'ecv',
'ecv'],
'col5': ['PHL',
'PHL',
'BHL',
'ALT',
'MRS',
'MRS',
'TUL',
'MRS',
'FAT',
'PHL',
'PHL',
'JEN',
'FTW',
'AMB',
'KGP'],
'col6': ['CP',
'CO',
'CP',
'CO',
'CP',
'CO',
'CO',
'CO',
'RT',
'CO',
'CO',
'CO',
'CP',
'CO',
'CO'],
'col7': ['PHL',
'PHL',
'ALT',
'ALT',
'MRS',
'TUL',
'MRS',
'FAT',
'FAH',
'PHL',
'JEN',
'FTW',
'AMB',
'KGP',
'KGP'],
'col8': ['CO',
'CO',
'CO',
'CO',
'CO',
'CO',
'CO',
'RT',
'CP',
'CO',
'CO',
'CP',
'CO',
'CO',
'CO'],
'col9': ['SID',
'M/M',
'SID',
'U/D',
'AL LO',
'AL LO',
'AL LO',
'AL LO',
'AL LO',
'M/M',
'DCS',
'DCS',
'DCS',
'DCS',
'DCS'],
'col10': ['SID',
'M/M',
'SID',
'U/D',
'AL LO',
'3 M',
'3 M',
'M/M',
'AL LO',
'M/M',
'DCS',
'DCS',
'DCS',
'DCS',
'DCS'],
'col11': [nan,
'ATM',
nan,
'PACK',
'AL LP',
'DCS',
'DCS',
'DAM',
'DAM',
'DCS',
'DCS',
'DCS',
'DCS',
'DCS',
'M/M'],
'col12': [nan,
'SID',
nan,
'PACK',
'CAL LO',
'DCS',
'DCS',
'M/M',
'CAL LO',
'DCS',
'DCS',
'DCS',
'DCS',
'DCS',
'AL LO'],
'col13': ['abc',
'-02-1_',
'-1',
'-13_',
nan,
nan,
nan,
'T1_VT1.',
nan,
'-06',
nan,
nan,
nan,
nan,
'-03_02-03'],
'col14': [nan,
nan,
nan,
nan,
'102/',
'102/',
'102/',
nan,
'101/',
nan,
'3405',
'3102/',
'3111/',
'3102/',
nan]}
df = pd.DataFrame(d)