Records for Pandas(1): Basic function and property of Series

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt#*******************creat Series**************************************************)# create a Series with an arbitrary lists = pd.Series([7,Heisenberg,3.14,-1789710578, Happy Eating!])print(s)# specify an index to Seriess = pd.Series([7,Heisenberg,3.14,-1789710578, Happy Eating!], index=[A, Z, C, Y, E])print(s,s.index,s.name,s.dtype)# dict----> Seriesd = {Chicago: 1000, New York: 1300, Portland: 900, San Francisco: 1100, Austin: 450, Boston: None} # a dict: d# dict as an input for Seriescities = pd.Series(d) # convert dic to Serise,print("dict-->Series:")cities.name = popu # add a name for Series: title of the valuecities.index.name = city # add a name for index of Series: title of indexprint(cities)print(
)#*******************select function*********************************************)# select function: # index for selectionprint(cities[New York])print(cities[[Chicago, Portland, San Francisco]]) # note the number of []# boolean indexing for selection:print(cities[cities < 1000])# Ture/False values for selection: less_than_1000 = cities < 1000print(True or False for value<1000:)print(less_than_1000)# True is the right elements we wantprint(
) # next lineprint(cities[less_than_1000])print(
)#*******************alter function**********************************************)# change the value:# changing based on the indexprint(old value:,cities[Chicago])print(new value:,cities[Chicago])# changing values using boolean logicprint(info for cities<1000:)print(cities[cities<1000])print(
)cities[cities<1000] = 750.999print(undated info for cities<1000:)print(cities[cities<1000])print(cities)print(
)#*******************in or out the Series****************************************)print(Seattle in cities) # False print(Boston in cities) # Trueprint(Boston not in cities) # False#*******************mathematical operation**************************************)print(np.sqrt(cities))# add tow Series together:print(cities[[Chicago, New York, Portland]])print(
)print(cities[[Austin, New York]])print(
)print(cities[[Chicago, New York, Portland]] + cities[[Austin, New York]])# Note:Values on either Series that did not have a shared index# will produce a NULL/NaN (not a number).#******************* NULL checking: isnull or notnull **************************)print(cities.isnull()) # return boolean logic valueprint(cities.notnull()) # note the ()print(
)# select element with null valueprint(cities[cities.isnull()])

推薦閱讀:

TAG:Python入門 | 數據挖掘入門 | 大數據分析 |