为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

POPMUISE 2022-07-19 15:23:18
我有以下数据框:这是我的代码:movies_taxes['Total Taxes'] = movies_taxes.apply(lambda x:(0.2)* x['US Gross'] + (0.18) * x['Worldwide Gross'], axis=1)movies_taxes
查看完整描述

3 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超8个赞

简单的例子:


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


查看完整回答
反对 回复 2022-07-19
?
哔哔one

TA贡献1854条经验 获得超8个赞

这应该这样做:


>>> 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


查看完整回答
反对 回复 2022-07-19
?
守候你守候我

TA贡献1802条经验 获得超10个赞

另一个例子:


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)

Pandas 0.17+(如其他答案中所述):


上升

df.sort_values('student')

逆升

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


查看完整回答
反对 回复 2022-07-19
  • 3 回答
  • 0 关注
  • 166 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号