pandas frequently used functions quickly check

# - * - Coding: UTF-8 - * - 
'' ' 
Author: When Yadong 
function: pandas Application 
Version: 
Time: 2019-10-01 
' '' 

# module import 
Import PANDAS AS pd
 Import numpy AS NP
 Import matplotlib. AS PLT pyplot 


'' ' Serise data types - one-dimensional data ' '' 

a = pd.Series ([. 1, 0.3 , np.nan]) 
B = pd.Series (np.array ([. 1, 2,. 3 ] )) 

Print ( ' A \ n- ' , A)
 Print ( ' B \ n- ' , B) 

# modified index 
A = pd.Series ([. 1, 0.3, np.nan], index = [ 'a' , ' B ' , ' C ' ])
 Print (A) 


' '' DataFrame data type - two-dimensional data '' ' 

# DATE_RANGE () randomly generates a time series 
DATE = pd.date_range ( ' 20,191,001 ' , periods. 5 = )
 # print (DATE) 

# create objects using numpy 
, index = DATE, Columns = List (DF = pd.DataFrame (np.random.randn (. 5,. 4) ' ABCD ' ))
 # print (DF) 

# View data 
print (df.head ())         # get the first few rows, five rows before returning to the default 
Print (df.tail ())        # Get after a few rows, five rows to return after default 
Print (df.index)          # Gets the index 
# Print (List (df.index)) 
Print (df.columns)        # Gets column name 
Print (df.values)         # Get all value 
Print (df.describe ())     # acquires description information 
Print (df.T)              # transpose 
Print (df.sort_index (Axis =. 1, Ascending = False))    # of index objects reordering 
Print (df.sort_values ( = by ' D ' ))                      # sort the elements of a column for 
Print ( ' *' * 50 ) 

# Select Data 
Print (DF [ ' A ' ])                       # get all the data for a column 
Print (DF [1: 3])                       # Gets the index 1: 3 line data 
Print (DF [ ' 20,191,001 ' : ' 20191004 ' ])     # Gets the index value of '20191001': '20191004' line data 
Print ( ' * ' * 50 ) 

# LOC positioning element method 
Print (df.loc [date [0]])               # acquisition date first index data 
Print (df.loc [:, [' A ' , ' B ' ]])         # Gets column named A, B of all the line data 
Print (df.loc [ ' 20,191,002 ' : ' 20,191,004 ' , [ ' A ' , ' B ' ]])      # Gets the index value of '20191002': '20191004' range of a, column B data 
Print (df.loc [ ' 20191002 ' , [ ' a ' , ' B ' ]])                # Gets the index value of '20191002'A, B, column data 
Print ( '* ' * 50 ) 

# get the data Boolean 
Print (DF [df.A> 0])      # acquired data in column A is larger than 0 
Print (DF [DF> 0])        # obtain all the data is greater than 0 



# assignment 
# Print (DF) 
S1 = pd.Series ([1,2,3,4], index = pd.date_range ( ' 20,191,002 ' , = periods. 4))    # generates a data type Series 
# Print ( 'S1 \ n-', s1) 

DF [ ' F. ' ] = s1             # added back to s1 DF 
# RINT ( 'DF \ n-', DF) 

df.at [DATE [0], ' A ' ] = 0   #Alternatively data in the specified table 
# Print ( 'DF \ n-', DF) 

df.loc [:, ' D ' ([. 5] * len (DF))] = np.array         # specified value in a column to be replaced , array type 
# Print ( 'DF \ n-', DF) 

#   processing NaN worth embodiment 
Print (df.dropna (= How ' the any ' ))    # deletes all of the data rows containing NaN 

Print (df.fillna (value =. 3) )      # default value filled NaN3 

Print (pd.isnull (DF))             # determines whether or not containing NaN, returns a Boolean value

 

Guess you like

Origin www.cnblogs.com/syd123/p/11618507.html