Small meatballs stepping into Python's path: python_day06 (another structure series in the Pandas Library)

Source: Internet
Author: User
Tags sin

write in front:

by yesterday's record we know, pandas.read_csv (" file name ") method to read the file, the variable type returned is dataframe structure . Also pandas one of the most core types in . That in pandas there is no other type Ah, of course there are, we put dataframe type is understood to be data consisting of rows and columns, then dataframe is decomposed to take one or more of the rows or columns, then one of the rows or columns is called series structure.

  that is, the dataframe structure is composed of a series of series structures .

(The DataFrame is understood as a read matrix, then the Series is considered to be a row or column of the matrix)

Series (collection of values)
DataFrame (Collection of Series objects)

feel the type directly in the form of code:
#data set is a rating on a movieImportPandas as Pdfandango= Pd.read_csv ("Fandango_score_comparison.csv")#use Pd.read_csv () to read the data about the movie score and assign it to a variable fandangoPrint("the types of Fandango are:", type (Fandango))#the type of the return variable fandango is dataframe typeSeries_film= fandango["FILM"]#assigns a column named "FILM" to the fandango, assigning a value to the variable series_filmPrint("the types of series_film are:", type (series_film))#the type of the return variable series_film is series type

Operation Result:

One, about the operation of series 1. To take a series of specific data directly from a slice
Series_film = fandango["FILM"]#assigns a column named "FILM" to the fandango, assigning a value to the variable series_filmPrint(Series_film[0:4])#Print the first 4 lines of series_film dataPrint("-------------------------------------------") Series_rt= fandango["RottenTomatoes"]#score for this rating sitePrint(Series_rt[0:4])

Operation Result:

2. What is the type of a value that takes a series type of data?

On top we know that a row or column of data of the Dataframe type would be series type, so what kind of data would you get if you took the values of series?

 from Import  = fandango["FILM"]            # Take "FILM" this column of data film_name = Series_ Film.values         # use. Values to get series_film corresponding data (movie name)print("   ", Film_name)print("", type (film_name))

Operation Result:

That is: The structure of theDataFrame is a series, and the structure of the series is Ndarray.

  Pandas is encapsulated on the NumPy , and many operations are a combination of NumPy operations.

   You can also point to the index of values to come and go a value for a particular series.

Film_name_one = series_film.values[1]print(film_name_one)                      #  Of course, you can also print the value of the specified index in the movie name.

Operation Result:

3. Create a new series and invoke the data with the STIRNG type index
#build a series of yourself fromPandasImportSeriesseries_film= fandango["FILM"]#take the "FILM" column of dataSeries_rt = fandango["RottenTomatoes"]#score for this rating siteFilm_name= Series_film.values#The Film_name variable is the data in the movie nameRt_scores = Series_rt.values#rt_scores variable is the data of the scoreSeries_custom= Series (rt_scores, index = film_name)#build a series with the score of the movie name and the RottenTomatoes websitePrint("the type of Series_custom to be re-assembled =", type (Series_custom))#returns the type of Series_customPrint("-------------------------------------------------------------------------------")Print(Series_custom[0:3])#return the first three rows of series_custom with slice [0:3]Print("-------------------------------------------------------------------------------")Print("with series_custom[[0]] =", Series_custom[[0]])#returns the first row of series type with the index of type intPrint("with Series_custom[[\ "Avengers:age of Ultron () \"] =", series_custom[["Avengers:age of Ultron"]])                                                        #the string type of index still returns to the first row of the series type

Operation Result:

 From the running results, the combined Series_custom type is also serise type, and the same data is returned with index = [0] and with index = "Avengers:age of Ultron (2015)". Indicates that the index of a series can be not only of type int, but also of type string.

Sort of 4.Series. Sort ()4.1
original_index = Series_custom.index.tolist () # print Series_custom index list of this series type  print   (original_index)  print  ( " --------------------------------------------------- ------------  " ) Sorted_index  = Sorted (original_index) #   print   (Sorted_index) sorted_ By_index  = Series_custom.reindex (sorted_index) #   Replace the reordered data with the new index  print  (Sorted_by_index) 

Operation Result:

The original index list

List of sorted index

Re-swapped the data after index

4.2 Sorting by different means
SC2 = Series_custom.sort_index ()          # sorted by Series_custom index print(sc2[0:5]) Print ("-------------------------------------------"= series_custom.sort_values ()         # Sort by series_custom value print(Sc3[0:5])

Operation Result:

5. Add (), sin (), Max () action on Sercies
#. Add () Two series addImportNumPy as NPPrint(Np.add (Series_custom,series_custom))#Add the same dimensions separatelyPrint("----------------------------------------------------")#Use the sin () function for each valuePrint(Np.sin (series_custom))Print("----------------------------------------------------")#returns the maximum valuePrint("max (series_custom) =", Np.max (Series_custom))

Operation Result:

The result of the Add ()

The result of finding sin ()

Ask Max ()

6. Output Serise_custom rated >50,<75 movies
Criteria_one = series_custom >           # Returns the result is True or falescriteria_two = Series_custom < the#< /c4>Print (criteria_one)Both_criteria = Series_custom[criteria_one & Criteria_two]    #  >50,<75 data print(Both_criteria)

Operation Result:

7. Operations between two series structures with the same index

Mr. Series_custom into two new series (the same concept as the previous one), and then two series with the same index, you can do the operation.

The index of the two series here is "FILM" (the movie name), and the values are the scores of two different scoring sites.

The original data:

#generate two new Serise,index the same, all are "FILM", values are the respective ratings of two websitesRt_critics = Series (fandango["rottent,omatoes"].values, index = fandango["FILM"]) Rt_users= Series (fandango["Rottentomatoes_user"].values, index = fandango["FILM"]) Rt_mean= (Rt_critics + rt_users)/2#The index of the two series is the same and can be manipulatedPrint(Rt_mean)

Run Result ":

8. For Dataframe type Set_index () (Specified index)
# returns a new dataframes that is indexed by the values in the specified column, and removes the column from Dataframe without deleting the film column fandango = pd.read_csv ("Fandango _score_comparison.csv")           print(Type (fandango))                         #  Read the file and assign it to a variable, print the variable type, for dataframe type fandango_films = Fandango.set_index ("FILM" , drop = False)      # use. Set_index () to dataframe Specify the index "FILM"print(fandango_ Films.index)      

Operation Result:

9. The slice of the index can be [1:5] This type, or it can be ["str": "str"] Type
# the slice of an index can be not just numeric, it can also be a string of type fandango_films["avengers:age of Ultron" " Hot Tub Time Machine 2 " ]#fandango_films.loc["Avengers:age of Ultron": "Hot Tub Time Machine 2"]

Operation Result:

Small meatballs stepping into Python's path: python_day06 (another structure series in the Pandas Library)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.