Pandas Sort DataFrame, ignoring case

I have a Pandas dataframe in Python. The contents of the data frame are here . I slightly modified the case of the first alphabet in the "One" column. Here is what I have:

import pandas as pd
df = pd.read_csv('test.csv')
print df

Position                       Artist                  Single               Year     Weeks
       1                Frankie Laine               I Believe               1953  18 weeks
       2                  Bryan Adams         I Do It for You               1991  16 weeks
       3                  Wet Wet Wet      love Is All Around               1994  15 weeks
       4  Drake (feat. Wizkid & Kyla)               One Dance               2016  15 weeks
       5                        Queen       bohemian Rhapsody  1975/76 & 1991/92  14 weeks
       6                 Slim Whitman              Rose Marie               1955  11 weeks
       7              Whitney Houston  i Will Always Love You               1992  10 weeks

I would like to sort the Single column in ascending order (from a to z). When i started

df.sort_values(by='Single',inplace=True)

It seems that sorting cannot combine upper and lower case. Here is what I get:

Position                       Artist                  Single               Year     Weeks
       1                Frankie Laine               I Believe               1953  18 weeks
       2                  Bryan Adams         I Do It for You               1991  16 weeks
       4  Drake (feat. Wizkid & Kyla)               One Dance               2016  15 weeks
       6                 Slim Whitman              Rose Marie               1955  11 weeks
       5                        Queen       bohemian Rhapsody  1975/76 & 1991/92  14 weeks
       7              Whitney Houston  i Will Always Love You               1992  10 weeks
       3                  Wet Wet Wet      love Is All Around               1994  15 weeks

, , . , . " " . ; 5- .

Pandas DataFrame, ?

+4
2

/ , argsort(), :

df.iloc[df.Single.str.lower().argsort()]

enter image description here

+5

Single :

df["Single.Upper"] = df["Single"].str.upper()
df.sort_values(by="Single.Upper", inplace=True)

:

del df["Single.Upper"] 
+1

Source: https://habr.com/ru/post/1666812/


All Articles