Is there a way to sort a pandas dataframe by column of numerical strings?

Firenze

Suppose my dataframe df is as below

value_id = ['1_1', '1_2', '1_5', '10_1', '2_2', '3_2']
value_age = [28, 34, 54, 24, 12, 56]
df = pd.DataFrame({'id':value_id, 'age':value_age})

I want to sort this dataframe according to the first column (i.e. id). I want an output like this

id     age
1_1    28
1_2    34
1_5    54
2_2    12
3_2    56
10_1   24
Ch3steR

You can set id as index and use df.reindex, and use sorted with custom key parameter.

def f(x):
    v, v1 = map(int, x.split('_'))
    return v, v1

df.set_index('id').reindex(sorted(df.id,key=f))

      age
id
1_1    28
1_2    34
1_5    54
2_2    12
3_2    56
10_1   24

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

numerical sort a column containing numbers and strings (pandas/python)

From Dev

Pandas DataFrame column numerical integration

From Dev

extracting numerical information from strings in a dataframe column

From Dev

Mapping strings in dataframe column to numerical values

From Dev

How to sort a pandas dataframe by a column that has both numbers and strings?

From Dev

Pandas Dataframe sort by a column

From Dev

Sort numerical column

From Dev

Sort on column and numerical value

From Dev

Sort pandas dataframe by customize way

From Dev

Sort a pandas DataFrame by a column in another dataframe - pandas

From Dev

How can I sort a column of strings in pandas dataframe where I force the order of the letters the column is sorted by?

From Dev

Replacing numerical IDs contained as lists of strings and lists of ints in a Pandas DataFrame

From Java

Sort Pandas Dataframe by substrings of a column

From Dev

Sort lists in a Pandas Dataframe column

From Dev

Sort dataframe by first column, Pandas

From Dev

Pandas DataFrame Sort every Column

From Dev

sort pandas DataFrame with a column with list

From Dev

Pad a column of strings in a pandas dataframe

From Dev

Pandas dataframe | dictionary inside column | round and sort within column | Best way to do it?

From Dev

Is there a way to create a pandas dataframe column based on current sort position and another column?

From Dev

Store values that are only numerical or alphabetical in Pandas dataframe column?

From Dev

Pandas Dataframe: Sort list column in dataframe

From Dev

Sort date in list of strings in pandas column

From Dev

Pandas DataFrame: How do I create numerical values out of numerical values from another column?

From Dev

How to numerical sort by last column?

From Dev

Sort values by numerical range in pandas

From Dev

Fastest way to sort each row in a pandas dataframe

From Python

How to sort pandas dataframe in a special way

From Dev

Sort pandas dataframe column based on substring

Related Related

  1. 1

    numerical sort a column containing numbers and strings (pandas/python)

  2. 2

    Pandas DataFrame column numerical integration

  3. 3

    extracting numerical information from strings in a dataframe column

  4. 4

    Mapping strings in dataframe column to numerical values

  5. 5

    How to sort a pandas dataframe by a column that has both numbers and strings?

  6. 6

    Pandas Dataframe sort by a column

  7. 7

    Sort numerical column

  8. 8

    Sort on column and numerical value

  9. 9

    Sort pandas dataframe by customize way

  10. 10

    Sort a pandas DataFrame by a column in another dataframe - pandas

  11. 11

    How can I sort a column of strings in pandas dataframe where I force the order of the letters the column is sorted by?

  12. 12

    Replacing numerical IDs contained as lists of strings and lists of ints in a Pandas DataFrame

  13. 13

    Sort Pandas Dataframe by substrings of a column

  14. 14

    Sort lists in a Pandas Dataframe column

  15. 15

    Sort dataframe by first column, Pandas

  16. 16

    Pandas DataFrame Sort every Column

  17. 17

    sort pandas DataFrame with a column with list

  18. 18

    Pad a column of strings in a pandas dataframe

  19. 19

    Pandas dataframe | dictionary inside column | round and sort within column | Best way to do it?

  20. 20

    Is there a way to create a pandas dataframe column based on current sort position and another column?

  21. 21

    Store values that are only numerical or alphabetical in Pandas dataframe column?

  22. 22

    Pandas Dataframe: Sort list column in dataframe

  23. 23

    Sort date in list of strings in pandas column

  24. 24

    Pandas DataFrame: How do I create numerical values out of numerical values from another column?

  25. 25

    How to numerical sort by last column?

  26. 26

    Sort values by numerical range in pandas

  27. 27

    Fastest way to sort each row in a pandas dataframe

  28. 28

    How to sort pandas dataframe in a special way

  29. 29

    Sort pandas dataframe column based on substring

HotTag

Archive