Python数据分析与机器学习-Pandas_4

# Series (collection of values)
# DataFrame (collection of Series 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
import pandas as pd
fandango = pd.read_csv('fandango_score_comparison.csv')
series_film = fandango['FILM']
print(series_film[0:5])
series_rt = fandango['RottenTomatoes']
print(series_rt[0:5])
0    Avengers: Age of Ultron (2015)
1                 Cinderella (2015)
2                    Ant-Man (2015)
3            Do You Believe? (2015)
4     Hot Tub Time Machine 2 (2015)
Name: FILM, dtype: object
0    74
1    85
2    80
3    18
4    14
Name: RottenTomatoes, dtype: int64
# Import the Series object from pandas
from pandas import Series
file_names = series_film.values
print(type(file_names))
#print(file_names)
rt_sources = series_rt.values
# print(rt_sources)
series_custom = Series(rt_sources,index=file_names)
series_custom[['Minions (2015)','Leviathan (2014)']]
# print(type(series_custom)
<class 'numpy.ndarray'>





Minions (2015)      54
Leviathan (2014)    99
dtype: int64
# int index is also aviable
series_custom = Series(rt_sources, index=file_names)
series_custom[['Minions (2015)','Leviathan (2014)']]
fiveten = series_custom[5:10]
print(fiveten)
The Water Diviner (2015)        63
Irrational Man (2015)           42
Top Five (2014)                 86
Shaun the Sheep Movie (2015)    99
Love & Mercy (2015)             89
dtype: int64
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("------")
print(sorted_by_index)
["'71 (2015)", '5 Flights Up (2015)', 'A Little Chaos (2015)', 'A Most Violent Year (2014)', 'About Elly (2015)', 'Aloha (2015)', 'American Sniper (2015)', 'American Ultra (2015)', 'Amy (2015)', 'Annie (2014)', 'Ant-Man (2015)', 'Avengers: Age of Ultron (2015)', 'Big Eyes (2014)', 'Birdman (2014)', 'Black Sea (2015)', 'Black or White (2015)', 'Blackhat (2015)', 'Cake (2015)', 'Chappie (2015)', 'Child 44 (2015)', 'Cinderella (2015)', 'Clouds of Sils Maria (2015)', 'Danny Collins (2015)', 'Dark Places (2015)', 'Do You Believe? (2015)', 'Dope (2015)', 'Entourage (2015)', 'Escobar: Paradise Lost (2015)', 'Ex Machina (2015)', 'Fantastic Four (2015)', 'Far From The Madding Crowd (2015)', 'Fifty Shades of Grey (2015)', 'Focus (2015)', 'Furious 7 (2015)', 'Get Hard (2015)', 'Gett: The Trial of Viviane Amsalem (2015)', 'Hitman: Agent 47 (2015)', 'Home (2015)', 'Hot Pursuit (2015)', 'Hot Tub Time Machine 2 (2015)', "I'll See You In My Dreams (2015)", 'Infinitely Polar Bear (2015)', 'Inherent Vice (2014)', 'Inside Out (2015)', 'Insidious: Chapter 3 (2015)', 'Into the Woods (2014)', 'Irrational Man (2015)', 'It Follows (2015)', 'Jupiter Ascending (2015)', 'Jurassic World (2015)', 'Kingsman: The Secret Service (2015)', 'Kumiko, The Treasure Hunter (2015)', 'Leviathan (2014)', 'Little Boy (2015)', 'Love & Mercy (2015)', 'Mad Max: Fury Road (2015)', 'Maggie (2015)', 'Magic Mike XXL (2015)', 'Maps to the Stars (2015)', 'Max (2015)', 'McFarland, USA (2015)', 'Me and Earl and The Dying Girl (2015)', 'Minions (2015)', 'Mission: Impossible – Rogue Nation (2015)', 'Monkey Kingdom (2015)', 'Mortdecai (2015)', 'Mr. Holmes (2015)', 'Mr. Turner (2014)', 'Night at the Museum: Secret of the Tomb (2014)', 'Paddington (2015)', 'Paper Towns (2015)', 'Paul Blart: Mall Cop 2 (2015)', 'Phoenix (2015)', 'Pitch Perfect 2 (2015)', 'Pixels (2015)', 'Poltergeist (2015)', 'Project Almanac (2015)', 'Red Army (2015)', 'Ricki and the Flash (2015)', 'Run All Night (2015)', 'Saint Laurent (2015)', 'San Andreas (2015)', 'Self/less (2015)', 'Selma (2014)', 'Serena (2015)', 'Seventh Son (2015)', 'Seymour: An Introduction (2015)', 'Shaun the Sheep Movie (2015)', 'Sinister 2 (2015)', 'Song of the Sea (2014)', 'Southpaw (2015)', 'Spare Parts (2015)', 'Spy (2015)', 'Still Alice (2015)', 'Straight Outta Compton (2015)', 'Strange Magic (2015)', 'Taken 3 (2015)', 'Tangerine (2015)', 'Ted 2 (2015)', 'Terminator Genisys (2015)', 'Testament of Youth (2015)', 'The 100-Year-Old Man Who Climbed Out the Window and Disappeared (2015)', 'The Age of Adaline (2015)', 'The Boy Next Door (2015)', 'The DUFF (2015)', 'The Diary of a Teenage Girl (2015)', 'The Divergent Series: Insurgent (2015)', 'The End of the Tour (2015)', 'The Gallows (2015)', 'The Gift (2015)', 'The Gunman (2015)', 'The Hobbit: The Battle of the Five Armies (2014)', 'The Hunting Ground (2015)', 'The Imitation Game (2014)', 'The Last Five Years (2015)', 'The Lazarus Effect (2015)', 'The Loft (2015)', 'The Longest Ride (2015)', 'The Man From U.N.C.L.E. (2015)', 'The Overnight (2015)', 'The Salt of the Earth (2015)', 'The Second Best Exotic Marigold Hotel (2015)', 'The SpongeBob Movie: Sponge Out of Water (2015)', 'The Stanford Prison Experiment (2015)', 'The Vatican Tapes (2015)', 'The Water Diviner (2015)', 'The Wedding Ringer (2015)', 'The Wolfpack (2015)', 'The Woman In Black 2 Angel of Death (2015)', 'The Wrecking Crew (2015)', 'Timbuktu (2015)', 'Tomorrowland (2015)', 'Top Five (2014)', 'Trainwreck (2015)', 'True Story (2015)', 'Two Days, One Night (2014)', 'Unbroken (2014)', 'Unfinished Business (2015)', 'Unfriended (2015)', 'Vacation (2015)', 'Welcome to Me (2015)', 'What We Do in the Shadows (2015)', 'When Marnie Was There (2015)', "While We're Young (2015)", 'Wild Tales (2014)', 'Woman in Gold (2015)']
------
'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
Ant-Man (2015)                                     80
Avengers: Age of Ultron (2015)                     74
Big Eyes (2014)                                    72
Birdman (2014)                                     92
Black Sea (2015)                                   82
Black or White (2015)                              39
Blackhat (2015)                                    34
Cake (2015)                                        49
Chappie (2015)                                     30
Child 44 (2015)                                    26
Cinderella (2015)                                  85
Clouds of Sils Maria (2015)                        89
Danny Collins (2015)                               77
Dark Places (2015)                                 26
Do You Believe? (2015)                             18
Dope (2015)                                        87
Entourage (2015)                                   32
Escobar: Paradise Lost (2015)                      52
Ex Machina (2015)                                  92
Fantastic Four (2015)                               9
                                                   ..
The Loft (2015)                                    11
The Longest Ride (2015)                            31
The Man From U.N.C.L.E. (2015)                     68
The Overnight (2015)                               82
The Salt of the Earth (2015)                       96
The Second Best Exotic Marigold Hotel (2015)       62
The SpongeBob Movie: Sponge Out of Water (2015)    78
The Stanford Prison Experiment (2015)              84
The Vatican Tapes (2015)                           13
The Water Diviner (2015)                           63
The Wedding Ringer (2015)                          27
The Wolfpack (2015)                                84
The Woman In Black 2 Angel of Death (2015)         22
The Wrecking Crew (2015)                           93
Timbuktu (2015)                                    99
Tomorrowland (2015)                                50
Top Five (2014)                                    86
Trainwreck (2015)                                  85
True Story (2015)                                  45
Two Days, One Night (2014)                         97
Unbroken (2014)                                    51
Unfinished Business (2015)                         11
Unfriended (2015)                                  60
Vacation (2015)                                    27
Welcome to Me (2015)                               71
What We Do in the Shadows (2015)                   96
When Marnie Was There (2015)                       89
While We're Young (2015)                           83
Wild Tales (2014)                                  96
Woman in Gold (2015)                               52
Length: 146, dtype: int64
sc2 = series_custom.sort_index()
sc3 = series_custom.sort_values()
print(sc2[0:10])
print("------")
print(sc3[0:10])
'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
# 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))
# Apply sin function to each value
np.sin(series_custom)
# Return the highest value
np.max(series_custom)
Avengers: Age of Ultron (2015)                    148
Cinderella (2015)                                 170
Ant-Man (2015)                                    160
Do You Believe? (2015)                             36
Hot Tub Time Machine 2 (2015)                      28
The Water Diviner (2015)                          126
Irrational Man (2015)                              84
Top Five (2014)                                   172
Shaun the Sheep Movie (2015)                      198
Love & Mercy (2015)                               178
Far From The Madding Crowd (2015)                 168
Black Sea (2015)                                  164
Leviathan (2014)                                  198
Unbroken (2014)                                   102
The Imitation Game (2014)                         180
Taken 3 (2015)                                     18
Ted 2 (2015)                                       92
Southpaw (2015)                                   118
Night at the Museum: Secret of the Tomb (2014)    100
Pixels (2015)                                      34
McFarland, USA (2015)                             158
Insidious: Chapter 3 (2015)                       118
The Man From U.N.C.L.E. (2015)                    136
Run All Night (2015)                              120
Trainwreck (2015)                                 170
Selma (2014)                                      198
Ex Machina (2015)                                 184
Still Alice (2015)                                176
Wild Tales (2014)                                 192
The End of the Tour (2015)                        184
                                                 ... 
Clouds of Sils Maria (2015)                       178
Testament of Youth (2015)                         162
Infinitely Polar Bear (2015)                      160
Phoenix (2015)                                    198
The Wolfpack (2015)                               168
The Stanford Prison Experiment (2015)             168
Tangerine (2015)                                  190
Magic Mike XXL (2015)                             124
Home (2015)                                        90
The Wedding Ringer (2015)                          54
Woman in Gold (2015)                              104
The Last Five Years (2015)                        120
Mission: Impossible – Rogue Nation (2015)       184
Amy (2015)                                        194
Jurassic World (2015)                             142
Minions (2015)                                    108
Max (2015)                                         70
Paul Blart: Mall Cop 2 (2015)                      10
The Longest Ride (2015)                            62
The Lazarus Effect (2015)                          28
The Woman In Black 2 Angel of Death (2015)         44
Danny Collins (2015)                              154
Spare Parts (2015)                                104
Serena (2015)                                      36
Inside Out (2015)                                 196
Mr. Holmes (2015)                                 174
'71 (2015)                                        194
Two Days, One Night (2014)                        194
Gett: The Trial of Viviane Amsalem (2015)         200
Kumiko, The Treasure Hunter (2015)                174
Length: 146, dtype: int64





100
# 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]
# print(series_greater_than_50)
criteria_one = series_custom > 50
criteria_two = series_custom < 75
both_criteria = series_custom[criteria_one & criteria_two]
print(both_criteria)
Avengers: Age of Ultron (2015)                                            74
The Water Diviner (2015)                                                  63
Unbroken (2014)                                                           51
Southpaw (2015)                                                           59
Insidious: Chapter 3 (2015)                                               59
The Man From U.N.C.L.E. (2015)                                            68
Run All Night (2015)                                                      60
5 Flights Up (2015)                                                       52
Welcome to Me (2015)                                                      71
Saint Laurent (2015)                                                      51
Maps to the Stars (2015)                                                  60
Pitch Perfect 2 (2015)                                                    67
The Age of Adaline (2015)                                                 54
The DUFF (2015)                                                           71
Ricki and the Flash (2015)                                                64
Unfriended (2015)                                                         60
American Sniper (2015)                                                    72
The Hobbit: The Battle of the Five Armies (2014)                          61
Paper Towns (2015)                                                        55
Big Eyes (2014)                                                           72
Maggie (2015)                                                             54
Focus (2015)                                                              57
The Second Best Exotic Marigold Hotel (2015)                              62
The 100-Year-Old Man Who Climbed Out the Window and Disappeared (2015)    67
Escobar: Paradise Lost (2015)                                             52
Into the Woods (2014)                                                     71
Inherent Vice (2014)                                                      73
Magic Mike XXL (2015)                                                     62
Woman in Gold (2015)                                                      52
The Last Five Years (2015)                                                60
Jurassic World (2015)                                                     71
Minions (2015)                                                            54
Spare Parts (2015)                                                        52
dtype: int64
# 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)
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
The Water Diviner (2015)                          62.5
Irrational Man (2015)                             47.5
Top Five (2014)                                   75.0
Shaun the Sheep Movie (2015)                      90.5
Love & Mercy (2015)                               88.0
Far From The Madding Crowd (2015)                 80.5
Black Sea (2015)                                  71.0
Leviathan (2014)                                  89.0
Unbroken (2014)                                   60.5
The Imitation Game (2014)                         91.0
Taken 3 (2015)                                    27.5
Ted 2 (2015)                                      52.0
Southpaw (2015)                                   69.5
Night at the Museum: Secret of the Tomb (2014)    54.0
Pixels (2015)                                     35.5
McFarland, USA (2015)                             84.0
Insidious: Chapter 3 (2015)                       57.5
The Man From U.N.C.L.E. (2015)                    74.0
Run All Night (2015)                              59.5
Trainwreck (2015)                                 79.5
Selma (2014)                                      92.5
Ex Machina (2015)                                 89.0
Still Alice (2015)                                86.5
Wild Tales (2014)                                 94.0
The End of the Tour (2015)                        90.5
                                                  ... 
Clouds of Sils Maria (2015)                       78.0
Testament of Youth (2015)                         80.0
Infinitely Polar Bear (2015)                      78.0
Phoenix (2015)                                    90.0
The Wolfpack (2015)                               78.5
The Stanford Prison Experiment (2015)             85.5
Tangerine (2015)                                  90.5
Magic Mike XXL (2015)                             63.0
Home (2015)                                       55.0
The Wedding Ringer (2015)                         46.5
Woman in Gold (2015)                              66.5
The Last Five Years (2015)                        60.0
Mission: Impossible – Rogue Nation (2015)       91.0
Amy (2015)                                        94.0
Jurassic World (2015)                             76.0
Minions (2015)                                    53.0
Max (2015)                                        54.0
Paul Blart: Mall Cop 2 (2015)                     20.5
The Longest Ride (2015)                           52.0
The Lazarus Effect (2015)                         18.5
The Woman In Black 2 Angel of Death (2015)        23.5
Danny Collins (2015)                              76.0
Spare Parts (2015)                                67.5
Serena (2015)                                     21.5
Inside Out (2015)                                 94.0
Mr. Holmes (2015)                                 82.5
'71 (2015)                                        89.5
Two Days, One Night (2014)                        87.5
Gett: The Trial of Viviane Amsalem (2015)         90.5
Kumiko, The Treasure Hunter (2015)                75.0
Length: 146, dtype: float64
posted @ 2019-07-03 08:07  Shinesu  阅读(395)  评论(0编辑  收藏  举报