如何按字母顺序对“标题”列中的数据框进行排序

时间:2020-02-05 19:33:45

标签: python

我有以下数据框:

这是我的代码:

movies_taxes['Total Taxes'] = movies_taxes.apply(lambda x:(0.2)* x['US Gross'] + (0.18) * x['Worldwide Gross'], axis=1)
movies_taxes

3 个答案:

答案 0 :(得分:0)

简单的例子:

import pandas as pd

df = pd.DataFrame({'player': ['C','B','A'], 'data': [1,2,3]})
df = df.sort_values(by ='player')

输出:

发件人:

  player  data
0      C     1
1      B     2
2      A     3

收件人:

  player  data
2      A     3
1      B     2
0      C     1

答案 1 :(得分:0)

另一个例子:

df = pd.DataFrame({
        'student': [
            'monica', 'nathalia', 'anastasia', 'marina', 'ema'
        ],

    'grade' : ['excellent', 'excellent', 'good', 'very good', 'good'
    ]
    })

    print (df)

                student   grade

        0       monica    excellent        
        1       nathalia  excellent         
        2       anastasia good        
        3       marina    very good          
        4       ema       good 

熊猫前0.17:

按升序排列学生姓名

df.sort('student')

反向上升

df.sort('student', ascending=False)

熊猫0.17+(如其他答案所述):

升序

df.sort_values('student')

反向上升

df.sort_values('student', ascending=False)

答案 2 :(得分:0)

这应该做到:

>>> import pandas as pd
>>> s = pd.Series(['banana', 'apple', 'friends', '3 dog and cat', '10 old man'])
>>> import numpy as np
# We want to know which rows start with a number as well as those that don't
>>> mask = np.array([True if not any(x.startswith(str(n)) for n in range(9)) else False for x in s])
>>> s[mask]
0     banana
1      apple
2    friends
dtype: object
# Stack the sorted, non-starting-with-a-number array and the sorted, starting-with-a-number array
>>> pd.concat((s[mask].sort_values(), s[~mask].sort_values(ascending=False)))
1            apple
0           banana
2          friends
3    3 dog and cat
4       10 old man