Pandas数据处理基本操作——Pandas的索引重置,索引与切片索引,自定义函数的应用

Pandas数据处理基本操作

以下是本次实验使用的数据表,如有需要联系,请留言
在这里插入图片描述

import pandas as pd
import numpy as np
#读取文件  不同媒体或网站对电影的评分
fandango = pd.read_csv('fandango_score_comparison.csv')
fandango.head()
FILMRottenTomatoesRottenTomatoes_UserMetacriticMetacritic_UserIMDBFandango_StarsFandango_RatingvalueRT_normRT_user_normMetacritic_normMetacritic_user_nomIMDB_normRT_norm_roundRT_user_norm_roundMetacritic_norm_roundMetacritic_user_norm_roundIMDB_norm_roundMetacritic_user_vote_countIMDB_user_vote_countFandango_votesFandango_Difference
0Avengers: Age of Ultron (2015)7486667.17.85.04.53.704.33.303.553.903.54.53.53.54.01330271107148460.5
1Cinderella (2015)8580677.57.15.04.54.254.03.353.753.554.54.03.54.03.524965709126400.5
2Ant-Man (2015)8090648.17.85.04.54.004.53.204.053.904.04.53.04.04.0627103660120550.5
3Do You Believe? (2015)1884224.75.45.04.50.904.21.102.352.701.04.01.02.52.531313617930.5
4Hot Tub Time Machine 2 (2015)1428293.45.13.53.00.701.41.451.702.550.51.51.51.52.5881956010210.5

重置索引值

可以看到在最左侧默认产生了索引,可以使用set_index重置索引,下面将FILM置为索引值

fandango.set_index('FILM').head()#将FILM置为索引后可以看到数据表中FILM被删除了  这是因为set_index的默认参数drop为True
RottenTomatoesRottenTomatoes_UserMetacriticMetacritic_UserIMDBFandango_StarsFandango_RatingvalueRT_normRT_user_normMetacritic_normMetacritic_user_nomIMDB_normRT_norm_roundRT_user_norm_roundMetacritic_norm_roundMetacritic_user_norm_roundIMDB_norm_roundMetacritic_user_vote_countIMDB_user_vote_countFandango_votesFandango_Difference
FILM
Avengers: Age of Ultron (2015)7486667.17.85.04.53.704.33.303.553.903.54.53.53.54.01330271107148460.5
Cinderella (2015)8580677.57.15.04.54.254.03.353.753.554.54.03.54.03.524965709126400.5
Ant-Man (2015)8090648.17.85.04.54.004.53.204.053.904.04.53.04.04.0627103660120550.5
Do You Believe? (2015)1884224.75.45.04.50.904.21.102.352.701.04.01.02.52.531313617930.5
Hot Tub Time Machine 2 (2015)1428293.45.13.53.00.701.41.451.702.550.51.51.51.52.5881956010210.5

将默认参数改为False

fandango.set_index('FILM',drop=False,inplace=True)
fandango.head()
FILMRottenTomatoesRottenTomatoes_UserMetacriticMetacritic_UserIMDBFandango_StarsFandango_RatingvalueRT_normRT_user_normMetacritic_normMetacritic_user_nomIMDB_normRT_norm_roundRT_user_norm_roundMetacritic_norm_roundMetacritic_user_norm_roundIMDB_norm_roundMetacritic_user_vote_countIMDB_user_vote_countFandango_votesFandango_Difference
FILM
Avengers: Age of Ultron (2015)Avengers: Age of Ultron (2015)7486667.17.85.04.53.704.33.303.553.903.54.53.53.54.01330271107148460.5
Cinderella (2015)Cinderella (2015)8580677.57.15.04.54.254.03.353.753.554.54.03.54.03.524965709126400.5
Ant-Man (2015)Ant-Man (2015)8090648.17.85.04.54.004.53.204.053.904.04.53.04.04.0627103660120550.5
Do You Believe? (2015)Do You Believe? (2015)1884224.75.45.04.50.904.21.102.352.701.04.01.02.52.531313617930.5
Hot Tub Time Machine 2 (2015)Hot Tub Time Machine 2 (2015)1428293.45.13.53.00.701.41.451.702.550.51.51.51.52.5881956010210.5

对多列索引查询

fandango[['FILM','RottenTomatoes']].head()
FILMRottenTomatoes
FILM
Avengers: Age of Ultron (2015)Avengers: Age of Ultron (2015)74
Cinderella (2015)Cinderella (2015)85
Ant-Man (2015)Ant-Man (2015)80
Do You Believe? (2015)Do You Believe? (2015)18
Hot Tub Time Machine 2 (2015)Hot Tub Time Machine 2 (2015)14

对多行索引查询

fandango.loc[['Avengers: Age of Ultron (2015)','Hot Tub Time Machine 2 (2015)']]
FILMRottenTomatoesRottenTomatoes_UserMetacriticMetacritic_UserIMDBFandango_StarsFandango_RatingvalueRT_normRT_user_normMetacritic_normMetacritic_user_nomIMDB_normRT_norm_roundRT_user_norm_roundMetacritic_norm_roundMetacritic_user_norm_roundIMDB_norm_roundMetacritic_user_vote_countIMDB_user_vote_countFandango_votesFandango_Difference
FILM
Avengers: Age of Ultron (2015)Avengers: Age of Ultron (2015)7486667.17.85.04.53.74.33.303.553.903.54.53.53.54.01330271107148460.5
Hot Tub Time Machine 2 (2015)Hot Tub Time Machine 2 (2015)1428293.45.13.53.00.71.41.451.702.550.51.51.51.52.5881956010210.5

对多行切片查询 设定步长 默认步长为1

#对多行切片查询  设定步长  默认步长为1
fandango.loc['Avengers: Age of Ultron (2015)':'Hot Tub Time Machine 2 (2015)':2]
FILMRottenTomatoesRottenTomatoes_UserMetacriticMetacritic_UserIMDBFandango_StarsFandango_RatingvalueRT_normRT_user_normMetacritic_normMetacritic_user_nomIMDB_normRT_norm_roundRT_user_norm_roundMetacritic_norm_roundMetacritic_user_norm_roundIMDB_norm_roundMetacritic_user_vote_countIMDB_user_vote_countFandango_votesFandango_Difference
FILM
Avengers: Age of Ultron (2015)Avengers: Age of Ultron (2015)7486667.17.85.04.53.74.33.303.553.903.54.53.53.54.01330271107148460.5
Ant-Man (2015)Ant-Man (2015)8090648.17.85.04.54.04.53.204.053.904.04.53.04.04.0627103660120550.5
Hot Tub Time Machine 2 (2015)Hot Tub Time Machine 2 (2015)1428293.45.13.53.00.71.41.451.702.550.51.51.51.52.5881956010210.5

查看数据类型

#查看数据类型
fandango.dtypes
FILM                           object
RottenTomatoes                  int64
RottenTomatoes_User             int64
Metacritic                      int64
Metacritic_User               float64
IMDB                          float64
Fandango_Stars                float64
Fandango_Ratingvalue          float64
RT_norm                       float64
RT_user_norm                  float64
Metacritic_norm               float64
Metacritic_user_nom           float64
IMDB_norm                     float64
RT_norm_round                 float64
RT_user_norm_round            float64
Metacritic_norm_round         float64
Metacritic_user_norm_round    float64
IMDB_norm_round               float64
Metacritic_user_vote_count      int64
IMDB_user_vote_count            int64
Fandango_votes                  int64
Fandango_Difference           float64
dtype: object

查看所有浮点型数据的索引

#查看所有浮点型数据的索引
fandango.dtypes[fandango.dtypes.values == 'float64'].index
Index(['Metacritic_User', 'IMDB', 'Fandango_Stars', 'Fandango_Ratingvalue',
       'RT_norm', 'RT_user_norm', 'Metacritic_norm', 'Metacritic_user_nom',
       'IMDB_norm', 'RT_norm_round', 'RT_user_norm_round',
       'Metacritic_norm_round', 'Metacritic_user_norm_round',
       'IMDB_norm_round', 'Fandango_Difference'],
      dtype='object')

修改数据类型

#修改数据类型
fandango['RottenTomatoes'].astype(np.float64,inplace = False).head()
0    74.0
1    85.0
2    80.0
3    18.0
4    14.0
Name: RottenTomatoes, dtype: float64

使用自定义函数计算float类型的列的分数标准差

#使用自定义函数计算float类型的列的分数标准差
#使用匿名函数在我的博文匿名函数lambda与过滤器filter中讲到
fandango[fandango.dtypes[fandango.dtypes.values == 'float64'].index].apply(lambda x:np.std(x)) 
Metacritic_User               1.505529
IMDB                          0.955447
Fandango_Stars                0.538532
Fandango_Ratingvalue          0.501106
RT_norm                       1.503265
RT_user_norm                  0.997787
Metacritic_norm               0.972522
Metacritic_user_nom           0.752765
IMDB_norm                     0.477723
RT_norm_round                 1.509404
RT_user_norm_round            1.003559
Metacritic_norm_round         0.987561
Metacritic_user_norm_round    0.785412
IMDB_norm_round               0.501043
Fandango_Difference           0.152141
dtype: float64

将所有的int64改为float64

#将所有的int64改为float64
fandango[fandango.dtypes[fandango.dtypes.values == 'int64'].index].astype(np.float64).head()
RottenTomatoesRottenTomatoes_UserMetacriticMetacritic_user_vote_countIMDB_user_vote_countFandango_votes
074.086.066.01330.0271107.014846.0
185.080.067.0249.065709.012640.0
280.090.064.0627.0103660.012055.0
318.084.022.031.03136.01793.0
414.028.029.088.019560.01021.0

计算每部电影RT_user_norm ,Metacritic_user_nom评分的标准差

#计算每部电影RT_user_norm ,Metacritic_user_nom评分的标准差
fandango.set_index('FILM').loc[:][['Metacritic_user_nom','RT_user_norm']].apply(lambda x:np.std(x),axis = 1).head()
FILM
Avengers: Age of Ultron (2015)    0.375
Cinderella (2015)                 0.125
Ant-Man (2015)                    0.225
Do You Believe? (2015)            0.925
Hot Tub Time Machine 2 (2015)     0.150
dtype: float64
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值