1.pandas.series
指定了對(duì)象Series使用自定義字符串索引
input
# Import the Series object from pandas
from pandas import Series
film_names = series_film.values
rt_scores = series_rt.values
series_custom = Series(rt_scores, index=film_names)
series_custom[['Minions (2015)', 'Leviathan (2014)']]
print(series_custom.head(5))
output
Avengers: Age of Ultron (2015) 74
Cinderella (2015) 85
Ant-Man (2015) 80
Do You Believe? (2015) 18
Hot Tub Time Machine 2 (2015) 14
dtype: int64
2.Reindexing
reindex()允許我們?yōu)閷?duì)象Series中的標(biāo)簽(索引)指定不同的順序。該方法接收與該系列對(duì)象所需的順序相對(duì)應(yīng)的字符串列表。
我們可以使用reindex()方法通過(guò)電影按字母順序排序series_custom从铲。要做到這一點(diǎn)衙熔,我們需要:
- 使用tolist()返回當(dāng)前索引的列表表示。
- 使用sorted()對(duì)索引進(jìn)行排序月帝。
- 使用reindex()設(shè)置新排序的索引躏惋。
input
original_index = series_custom.index
original_index_sorted = sorted(original_index)
sorted_by_index = series_custom.reindex(original_index_sorted)
print(sorted_by_index.head(10))
output
'71 (2015) 97
5 Flights Up (2015) 52
A Little Chaos (2015) 40
A Most Violent Year (2014) 90
About Elly (2015) 97
Aloha (2015) 19
American Sniper (2015) 72
American Ultra (2015) 46
Amy (2015) 97
Annie (2014) 27
dtype: int64
3.Sorting
input
sc2 = series_custom.sort_index()
sc3 = series_custom.sort_values()
print(sc2.head(10))
print('-----------------------')
print(sc3.head(10))
output
'71 (2015) 97
5 Flights Up (2015) 52
A Little Chaos (2015) 40
A Most Violent Year (2014) 90
About Elly (2015) 97
Aloha (2015) 19
American Sniper (2015) 72
American Ultra (2015) 46
Amy (2015) 97
Annie (2014) 27
dtype: int64
-----------------------
Paul Blart: Mall Cop 2 (2015) 5
Hitman: Agent 47 (2015) 7
Hot Pursuit (2015) 8
Fantastic Four (2015) 9
Taken 3 (2015) 9
The Boy Next Door (2015) 10
The Loft (2015) 11
Unfinished Business (2015) 11
Mortdecai (2015) 12
Seventh Son (2015) 12
dtype: int64
4.Comparing and Filtering
input
criteria_one = series_custom > 50
criteria_two = series_custom < 75
both_criteria = series_custom[criteria_one & criteria_two]
print(both_criteria.head(5))
output
Avengers: Age of Ultron (2015) 74
The Water Diviner (2015) 63
Unbroken (2014) 51
Southpaw (2015) 59
Insidious: Chapter 3 (2015) 59
dtype: int64
5.Alignment
input
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.head(5))
output
FILM
Avengers: Age of Ultron (2015) 80.0
Cinderella (2015) 82.5
Ant-Man (2015) 85.0
Do You Believe? (2015) 51.0
Hot Tub Time Machine 2 (2015) 21.0
dtype: float64