1

I am performing an image analysis, in which the output has the following format: format 1

in which "slice" refers to which sample corresponds

but I need it to be like this

format 2

where each row corresponds to a sample

I tried this code:

df['g'] = (df.groupby('Slice').cumcount() + 1).astype(str)
df1 = df.set_index(['Slice','g']).unstack()
df1.sort_index(axis=1,level=1, inplace=True)
df1.columns = [''.join(col) for col in df1.columns]
df1.reset_index(inplace=True)
df1 = df1.set_index("Slice")

but it groups the first value of each sample, which I don't need and i don't know how to fix it.

tcaceres
  • 13
  • 2
  • This should be what you are looking for: https://stackoverflow.com/questions/53818966/how-to-transpose-pandas-data-frame-by-a-column-and-value – lunastarwarp Mar 25 '21 at 18:31
  • Consider adding some comments (text) describing the formats that you want. They aren't very complicated.The Image links will be broken and your question will become useless to others once it is answered. – Adomas Baliuka Mar 25 '21 at 19:30

1 Answers1

0

Try this:

#dataframe
data = {
    'mean': np.random.randint(1590, 1650, 6),
    'slice': [1, 1, 1, 2, 2, 2]
}
df = pd.DataFrame(data)

Using groupby and pivot:

df['col'] = (df.groupby('slice', as_index=False)['slice'].cumcount()+1).astype(str).radd('Mean')
df.pivot(index='slice', columns='col').droplevel(axis=1, level=0)

output:

col     Mean1   Mean2   Mean3
slice           
1       1646    1621    1620
2       1630    1632    1621
ashkangh
  • 1,594
  • 1
  • 6
  • 9