Reputation: 644
My data is generated row wise using the separator "," and csv file is created and appended.i have four columns my data looks:
Image Maxval locx locy
123.jpg 0.99 56 78
223.jpg 0.95 54 71
221.jpg 0.93 54 77
123.jpg 0.92 66 77
223.jpg 0.94 56 79
221.jpg 0.97 57 72
Now if the image name is same then I want to append the data in columns so it should look like
Image Maxval locx locy Maxval locx locy
123.jpg 0.99 56 78 0.92 66 77
223.jpg 0.95 54 71 0.94 56 79
221.jpg 0.93 54 77 0.97 57 72
Here I am confused wheather I should use dataframes or some csv function.
The photo shows the column Maxval_1 is sorted but other columns Maxval_2, maxval_3 and Maxval_4 are not
Upvotes: 2
Views: 280
Reputation: 75080
Use groupby.cumcount
to assign a key k
and unstack
:
m=(df.assign(k=(df.groupby('Image').Maxval.cumcount()+1))
.set_index(['Image','k']).unstack().sort_values('k',axis=1).reindex(df.Image.unique())
m.columns=['_'.join(map(str,i)) for i in m.columns]
print(m)
Maxval_1 locx_1 locy_1 Maxval_2 locx_2 locy_2
Image
123.jpg 0.99 56 78 0.92 66 77
223.jpg 0.95 54 71 0.94 56 79
221.jpg 0.93 54 77 0.97 57 72
Upvotes: 3
Reputation: 2487
If you have an option store them as a list of values always keep appending to the list as using the same column name is not advised while storing as a CSV
file
Upvotes: 0