I have a df as follows.
TimeStamp,Value
t1,akak
t2,bb
t3,vvv
t5,ff
t6,44
t7,99
t8,kfkkf
t9,ff
t10,oo
I want to split df into sizes of 2 rows and assign class as group number.
TimeStamp,Value, class
t1,akak,c1
t2,bb,c1
t3,vvv,c2
t4,ff,c2
t5,44,c3
t6,99,c3
t7,kfkkf,c4
t8,ff,c4
t9,oo,c5
t10,oo,c5
One approach is to iterate and do it one at a time. Was thinking of there is inbuilt way in pandas to do it
CodePudding user response:
You could do:
df['class'] = [i//2 for i in range(len(df))]
But this is a pretty limited answer; you might want to apply a certain value on your other columns to get the group ID, or you may have a specific label in mind to apply for the class column, in which case you could follow up with a map function on the series to turn those numbers into something else.
CodePudding user response:
You can use this to achieve what you want:
df["class"] = [f"c{(i // 2) 1}" for i in range(df.shape[0])]
CodePudding user response:
Another possible solution:
df['class'] = ['c' str(int(x)) for x in np.repeat(np.arange(1, 1 len(df)/2), 2)]
Output:
TimeStamp Value class
0 t1 akak c1
1 t2 bb c1
2 t3 vvv c2
3 t4 ff c2
4 t5 ff c3
5 t6 44 c3
6 t7 99 c4
7 t8 kfkkf c4
8 t9 ff c5
9 t10 oo c5