#2.1.7 Pandas Internals: Series.md

1.pandas.series

指定了对象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()允许我们为对象Series中的标签(索引)指定不同的顺序。该方法接收与该系列对象所需的顺序相对应的字符串列表。
我们可以使用reindex()方法通过电影按字母顺序排序series_custom。要做到这一点,我们需要:

  • 使用tolist()返回当前索引的列表表示。
  • 使用sorted()对索引进行排序。
  • 使用reindex()设置新排序的索引。
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

你可能感兴趣的:(#2.1.7 Pandas Internals: Series.md)