pandas实现数据排序

一、数据读入

house_price = pd.read_csv('./data/house_price.csv')
print(house_price.head(3))
   Id  MSSubClass MSZoning  ...  SaleType  SaleCondition SalePrice
0   1          60       RL  ...        WD         Normal    208500
1   2          20       RL  ...        WD         Normal    181500
2   3          60       RL  ...        WD         Normal    223500

[3 rows x 81 columns]

二、数据排序

1、对Series进行排序

        用.sort_values()方法对Series进行排序,参数ascending可以指定正序排列还是倒叙排列,返回一个新的Series,不会对原数据造成影响,代码如下:

#默认参数从小到大排序
a =house_price['SalePrice'].sort_values()
print(a)
#指定参数后会按照从大到小排序,降序排列
b =house_price['SalePrice'].sort_values(ascending=False)
print(b)
495      34900
916      35311
968      37900
533      39300
30       40000
         ...  
803     582933
898     611657
1169    625000
1182    745000
691     755000
Name: SalePrice, Length: 1460, dtype: int64
691     755000
1182    745000
1169    625000
898     611657
803     582933
         ...  
30       40000
533      39300
968      37900
916      35311
495      34900
Name: SalePrice, Length: 1460, dtype: int64

可以看到,一个正序排列,一个倒叙排列,且前面的index还是原来数据对应的index。

2、对Dataframe排序

        依然可以用.sort_values()对整个Dataframe进行排序,同过by参数指定按照哪一列进行排序,也可以用ascending参数指定是正序排列还是倒叙排列。代码如下:

#按照单列升序排列
c = house_price.sort_values(by='SalePrice')
print(c)
#按照单列降序排列
d = house_price.sort_values(by='SalePrice',ascending=False)
print(d)
      Id  MSSubClass MSZoning  ...  SaleType  SaleCondition SalePrice
495  496          30  C (all)  ...        WD        Abnorml     34900
916  917          20  C (all)  ...        WD        Abnorml     35311
968  969          50       RM  ...        WD        Abnorml     37900
533  534          20       RL  ...        WD         Normal     39300
30    31          70  C (all)  ...        WD         Normal     40000

[5 rows x 81 columns]
        Id  MSSubClass MSZoning  ...  SaleType  SaleCondition SalePrice
691    692          60       RL  ...        WD         Normal    755000
1182  1183          60       RL  ...        WD        Abnorml    745000
1169  1170          60       RL  ...        WD         Normal    625000
898    899          20       RL  ...       New        Partial    611657
803    804          60       RL  ...       New        Partial    582933

[5 rows x 81 columns]

可以看到整个Dataframe就按照指定的那一列进行排序了。

        除了按照某一列进行排序外,还可以按照多列进行排序,在by参数中输入一个列表即可,同样的,ascending参数也需要给定一个列表来分别指定两列是正序排列还是倒叙排列,代码如下:

'''按照多列排序,先对前面的列进行排序,在前面列的值相等的时候,对后面的列进行排序'''
e = house_price.sort_values(by=['MSSubClass','SalePrice'])
print(e)
'''指定ascending参数后,对第一列升序排列,第二列降序排列'''
f = house_price.sort_values(by=['MSSubClass','SalePrice'],ascending=[True,False])
print(f)

运行结果如下:

      Id  MSSubClass MSZoning  ...  SaleType  SaleCondition SalePrice
916  917          20  C (all)  ...        WD        Abnorml     35311
533  534          20       RL  ...        WD         Normal     39300
812  813          20  C (all)  ...        WD         Alloca     55993
410  411          20       RL  ...       COD        Abnorml     60000
462  463          20       RL  ...        WD         Normal     62383

[5 rows x 81 columns]
        Id  MSSubClass MSZoning  ...  SaleType  SaleCondition SalePrice
898    899          20       RL  ...       New        Partial    611657
440    441          20       RL  ...        WD         Normal    555000
178    179          20       RL  ...       New        Partial    501837
1373  1374          20       RL  ...        WD         Normal    466500
1243  1244          20       RL  ...       New        Partial    465000

[5 rows x 81 columns]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

transuperb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值