Reputation: 589
I need to reorder a pandas dataframe with the second column:
colors numbers
0 red '1 - 49 ch'
1 white '10 - 490 ch'
3 blue '100 - 4900 ch'
4 green '2 - 3000 ch'
5 blue '10 - 4900 ch'
6 blue '1 - 49 ch'
This columns contains string that I need to split and order by the first element to have this result:
colors numbers
0 red '1 - 49 ch'
1 blue '1 - 49 ch'
2 green '2 - 3000 ch'
3 white '10 - 490 ch'
4 blue '10 - 4900 ch'
5 blue '100 - 4900 ch'
How can I looping thru the second column to get the result I need?
for i in df['numbers']:
print(i.split(" - ")[0])
Thank you
Upvotes: 3
Views: 74
Reputation: 354
If you want to use split you can add a new row with :
df['numbers2']=df.numbers.str.split('-').str[0].astype('int')
and then apply sort_values on this new column
Upvotes: 0
Reputation: 323226
Let us try index_natsorted
from natsort
from natsort import index_natsorted
df=df.iloc[index_natsorted(df.numbers)]
Upvotes: 2
Reputation: 150735
We can try extracting the numbers, and sort on them:
(df.join(df.numbers.str.extract('(\d+) - (\d+)').astype(int))
.sort_values([0,1], kind='mergesort')
.drop([0,1], axis=1)
)
Output:
colors numbers
0 red '1 - 49 ch'
6 blue '1 - 49 ch'
4 green '2 - 3000 ch'
1 white '10 - 490 ch'
5 blue '10 - 4900 ch'
3 blue '100 - 4900 ch'
Upvotes: 2