数据索引转换
博客专区 > leonhu 的博客 > 博客详情
数据索引转换
leonhu 发表于10个月前
数据索引转换
  • 发表于 10个月前
  • 阅读 12
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 对数据创建新的索引使得数据操作更直观

数据索引转换

对数据创建新的索引使得数据操作更直观

import pandas as pd
fandango = pd.read_csv('fandango_score_comparison.csv')
print(type(fandango))
<class 'pandas.core.frame.DataFrame'>
# drop = False 保留原有的列
# drop = True 不保留原有的列
fandango_films = fandango.set_index('FILM', drop=False)
# print(fandango_films[:3])
# fandango_films[:3]
# Slice using either bracket notation or loc[]
# fandango_films["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"]
loc= fandango_films.loc["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"]

import numpy as np

# returns the data types as a Series
types = fandango_films.dtypes
# print(types)
# filter data types to just floats, index attributes returns just column names
float_columns = types[types.values=='float64'].index
print('---')
print(float_columns[:2])
float_df = fandango_films[float_columns]
print('---')
print(float_df[:2])

# 'x' is a Series object representing a column
# 方差,每一列计算方差
deviations = float_df.apply(lambda x: np.std(x))[:5]
print('---')
print(deviations)
---
Index(['Metacritic_User', 'IMDB'], dtype='object')
---
                                Metacritic_User  IMDB  Fandango_Stars  \
FILM                                                                    
Avengers: Age of Ultron (2015)              7.1   7.8             5.0   
Cinderella (2015)                           7.5   7.1             5.0   

                                Fandango_Ratingvalue  RT_norm  RT_user_norm  \
FILM                                                                          
Avengers: Age of Ultron (2015)                   4.5     3.70           4.3   
Cinderella (2015)                                4.5     4.25           4.0   

                                Metacritic_norm  Metacritic_user_nom  \
FILM                                                                   
Avengers: Age of Ultron (2015)             3.30                 3.55   
Cinderella (2015)                          3.35                 3.75   

                                IMDB_norm  RT_norm_round  RT_user_norm_round  \
FILM                                                                           
Avengers: Age of Ultron (2015)       3.90            3.5                 4.5   
Cinderella (2015)                    3.55            4.5                 4.0   

                                Metacritic_norm_round  \
FILM                                                    
Avengers: Age of Ultron (2015)                    3.5   
Cinderella (2015)                                 3.5   

                                Metacritic_user_norm_round  IMDB_norm_round  \
FILM                                                                          
Avengers: Age of Ultron (2015)                         3.5              4.0   
Cinderella (2015)                                      4.0              3.5   

                                Fandango_Difference  
FILM                                                 
Avengers: Age of Ultron (2015)                  0.5  
Cinderella (2015)                               0.5  
---
Metacritic_User         1.505529
IMDB                    0.955447
Fandango_Stars          0.538532
Fandango_Ratingvalue    0.501106
RT_norm                 1.503265
dtype: float64
# 也可按行计算方差(例子数据没有实际意义)
rt_mt_user = float_df[['Metacritic_User','IMDB']]
rt_mt_user.apply(lambda x:np.std(x), axis=1)[:10]
FILM
Avengers: Age of Ultron (2015)    0.35
Cinderella (2015)                 0.20
Ant-Man (2015)                    0.15
Do You Believe? (2015)            0.35
Hot Tub Time Machine 2 (2015)     0.85
The Water Diviner (2015)          0.20
Irrational Man (2015)             0.35
Top Five (2014)                   0.15
Shaun the Sheep Movie (2015)      0.70
Love & Mercy (2015)               0.35
dtype: float64
标签: Python
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 37
码字总数 13436
×
leonhu
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: