# coding=utf-8
import pandas as pd
import numpy as np
from pandas import Series
'''
Series的方法与属性
属性:
values 获取某一列的数据值 获取的值为numpy.ndarray类型
index 获取series数据
方法:
Series(数值项,index=索引的项) #数值项 与索引的项必须一一对应 ,索引项可以为字符串
index.tolist() names
sort_index() 用索引项排序
sort_values() 用数值项排序
'''
#Series (collection of values)
#DataFrame (collection of Series objects)
#Panel (collection of DataFrame objects)
#A Series object can hold many data types, including
#float - for representing float values
#int - for representing integer values
#bool - for representing Boolean values
#datetime64[ns] - for representing date & time, without time-zone
#datetime64[ns, tz] - for representing date & time, with time-zone
#timedelta[ns] - for representing differences in dates & times (seconds, minutes, etc.)
#category - for representing categorical values
#object - for representing String values
#FILM - film name
#RottenTomatoes - Rotten Tomatoes critics average score
#RottenTomatoes_User - Rotten Tomatoes user average score
#RT_norm - Rotten Tomatoes critics average score (normalized to a 0 to 5 point system)
#RT_user_norm - Rotten Tomatoes user average score (normalized to a 0 to 5 point system)
#Metacritic - Metacritic critics average score
#Metacritic_User - Metacritic user average score
#Series相当与DataFrame的一行或一列
fandango = pd.read_csv('fandango_score_comparison.csv')
series_film = fandango['FILM'] #取出 FILM这一列
#print(type(series_film)) #class 'pandas.core.series.Series'
#print(series_film[0:5])
series_rt = fandango['RottenTomatoes']
#print(series_rt[0:5])
film_names = series_film.values #获取
#print(type(film_names))
rt_scores = series_rt.values
series_custom = Series(rt_scores , index=film_names)
series_custom[['Minions (2015)', 'Leviathan (2014)']] #以film_names为参数进行索引
fiveten = series_custom[5:10]
#print(fiveten)
#reindex更多的不是修改pandas对象的索引,而只是修改索引的顺序,
# 如果修改的索引不存在就会使用默认的None代替此行。且不会修改原数组,
# 要修改需要使用赋值语句。
original_index = series_custom.index.tolist()
#print(original_index)
sorted_index = sorted(original_index)
#print(sorted_index)
sorted_by_index = series_custom.reindex(sorted_index)
#print(sorted_by_index)
sc2 = series_custom.sort_index()
#print(sc2[0:10])
sc3 = series_custom.sort_values()
#print(sc3[0:10])
#The values in a Series object are treated as an ndarray, the core data type in NumPy
import numpy as np
# Add each value with each other
print(np.add(series_custom, series_custom)) #add 对Series的value数值项,求和
# Apply sine function to each value
np.sin(series_custom) #对series_custom 的Series的value求sin函数
# Return the highest value (will return a single value not a Series)
print(np.max(series_custom)) #求最大值
# 计算value在50~75之间的数值
#will actually return a Series object with a boolean value for each film
series_custom > 50
series_greater_than_50 = series_custom[series_custom > 50]
criteria_one = series_custom > 50
criteria_two = series_custom < 75
both_criteria = series_custom[criteria_one & criteria_two]
print(both_criteria)
#data alignment same index
#求两个数值的平均值
rt_critics = Series(fandango['RottenTomatoes'].values, index=fandango['FILM'])
rt_users = Series(fandango['RottenTomatoes_User'].values, index=fandango['FILM'])
rt_mean = (rt_critics + rt_users)/2
print(rt_mean)
# coding=utf-8
import pandas as pd
#will return a new DataFrame that is indexed by the values in the specified column
#and will drop that column from the DataFrame
#without the FILM column dropped
fandango = pd.read_csv('fandango_score_comparison.csv')
print(type(fandango))
fandango_films = fandango.set_index('FILM', drop=False) #以set_index作为索引
print(fandango_films.index)
# Slice using either bracket notation or loc[]
fandango_films["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"]
fandango_films.loc["Avengers: Age of Ultron (2015)":"Hot Tub Time Machine 2 (2015)"]
# Specific movie
fandango_films.loc['Kumiko, The Treasure Hunter (2015)']
# Selecting list of movies
movies = ['Kumiko, The Treasure Hunter (2015)', 'Do You Believe? (2015)', 'Ant-Man (2015)']
fandango_films.loc[movies]
#When selecting multiple rows, a DataFrame is returned,
#but when selecting an individual row, a Series object is returned instead
#-------------------------
#The apply() method in Pandas allows us to specify Python logic
#The apply() method requires you to pass in a vectorized operation
#that can be applied over each Series object.
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
# use bracket notation to filter columns to just float columns
float_df = fandango_films[float_columns]
#print float_df
# `x` is a Series object representing a column
deviations = float_df.apply(lambda x: np.std(x))
print(deviations)
rt_mt_user = float_df[['RT_user_norm', 'Metacritic_user_nom']]
rt_mt_user.apply(lambda x: np.std(x), axis=1)