Reputation: 67
I have the following dataframe:
name_1 name_2 Temp x1 x2
0 a b 293.35 0.0 1.0
0 a b 293.35 0.5 0.5
0 a b 293.35 1.0 0.0
0 a b 295.35 0.0 1.0
0 a b 295.35 0.5 0.5
0 a b 295.35 1.0 0.0
0 a b 297.35 0.0 1.0
0 a b 297.35 0.5 0.5
0 a b 297.35 1.0 0.0
1 c d 294.35 0.0 1.0
1 c d 294.35 0.5 0.5
1 c d 294.35 1.0 0.0
1 c d 296.35 0.0 1.0
1 c d 296.35 0.5 0.5
1 c d 296.35 1.0 0.0
1 c d 299.35 0.0 1.0
1 c d 299.35 0.5 0.5
1 c d 299.35 1.0 0.0
2 a c 300.35 0.0 1.0
2 a c 305.35 0.5 0.5
2 a c 310.35 1.0 0.0
I would like to arrange the data such that the Temp are repeating based on x1 and x2 to get this:
name_1 name_2 Temp x1 x2
0 a b 293.35 0.0 1.0
0 a b 295.35 0.0 1.0
0 a b 297.35 0.0 1.0
0 a b 293.35 0.5 0.5
0 a b 295.35 0.5 0.5
0 a b 297.35 0.5 0.5
0 a b 293.35 1.0 0.0
0 a b 295.35 1.0 0.0
0 a b 297.35 1.0 0.0
1 c d 294.35 0.0 1.0
1 c d 296.35 0.0 1.0
1 c d 299.35 0.0 1.0
1 c d 294.35 0.5 0.5
1 c d 296.35 0.5 0.5
1 c d 299.35 0.5 0.5
1 c d 294.35 1.0 0.0
1 c d 296.35 1.0 0.0
1 c d 299.35 1.0 0.0
2 a c 300.35 0.0 1.0
2 a c 305.35 0.5 0.5
2 a c 310.35 1.0 0.0
I got close using the following:
df['new'] = df.groupby('Temp')['Temp'].cumcount()
df.sort_values(by=['new', 'Temp', "name_1", "name_2"])
This code yields:
name_1 name_2 Temp x1 x2
0 a b 293.35 0.0 1.0
1 c d 294.35 0.0 1.0
0 a b 295.35 0.0 1.0
2 a c 300.35 0.0 1.0
The issue with this is that TRange is not being repeated based on name_1 and name_2 like the desired output.
Upvotes: 0
Views: 907
Reputation: 120419
Sort by x1
values then by index using kind='mergesort'
to ensure your values stay sorted:
>>> df.sort_values('x1', kind='mergesort').sort_index(kind='mergesort')
name_1 name_2 Temp x1 x2
0 a b 293.35 0.0 1.0
0 a b 295.35 0.0 1.0
0 a b 297.35 0.0 1.0
0 a b 293.35 0.5 0.5
0 a b 295.35 0.5 0.5
0 a b 297.35 0.5 0.5
0 a b 293.35 1.0 0.0
0 a b 295.35 1.0 0.0
0 a b 297.35 1.0 0.0
1 c d 294.35 0.0 1.0
1 c d 296.35 0.0 1.0
1 c d 299.35 0.0 1.0
1 c d 294.35 0.5 0.5
1 c d 296.35 0.5 0.5
1 c d 299.35 0.5 0.5
1 c d 294.35 1.0 0.0
1 c d 296.35 1.0 0.0
1 c d 299.35 1.0 0.0
2 a c 300.35 0.0 1.0
2 a c 305.35 0.5 0.5
2 a c 310.35 1.0 0.0
Upvotes: 1
Reputation: 260745
Simply sort on x1. You can specify a stable sorting algorithm:
df = df.sort_values(by='x1', kind='mergesort')
grouping and applying a cumcount is going nothing other than keeping the original order of the other parameters, which a stable sorting algorithm is already doing.
cf. sort_values
documentation for reference.
Upvotes: 1