<gh_stars>0
#Some basic methods and their application in pandas.
#Importing Pandas to use the methods.
import pandas as pd

#---------------------------------------------------------------------------------------------------

#Creating Data files - DataFrames & Series

#To create DataFrames.
first_df = pd.DataFrame({'A': [50, 21, 34, 99, 21, 231], 'B': [131, 2, 342, 563, 90, 88], 'C' : [12, 22, 55, 98, 94, 21], 'D' : [11, 236, 54, 75, 91, 20]})
print(first_df)
second_df = pd.DataFrame({'Bob': ['I liked it.', 'It was awful.'], 'Sue': ['Pretty good.', 'Bland.']}, 'Henry' : ['Not Bad.', 'Poor.'])
print(second_df)

#To create DataFrames using index.
third_df = pd.DataFrame({'Bob': ['I liked it.', 'It was awful.'], 
              'Sue': ['Pretty good.', 'Bland.']},
             index=['Product A', 'Product B'])
print(third_df)


#To create Series.
first_se = pd.Series([1, 2, 3, 4, 5])
print(first_se)

#To create Series using index.
second_se = pd.Series([30, 35, 40], index=['2015 Sales', '2016 Sales', '2017 Sales'], name='Product A')
print(second_se)


#Reading Data files - Comma Separated Values(CSV)
first_df.to_csv("first_df.csv")
print(first_df.shape)
print("\n")
print(first_df.head())

#--------------------------------------------------------------------------------------------------------

#Native accessors
print(first_df)

#To print the A column elements
print(first_df.A)
print(first_df['A']
print(first_df.C)
print(first_df['C']      
      
#To print a specific element of Yes column
print(first_df['A'][0])

   
      
#Indexing
#Index-based selection     
print(first_df.iloc[0])
      
#To get a column with iloc      
print(first_df.iloc[:, 0])  
print(first_df.iloc[:4, 0])      
print(first_df.iloc[1:4, 0])

#To pass a list
print(first_df.iloc[[0, 1, 2], 2])     

#To get last 3 elements
print(first_df.iloc[-3: ])    
      
      
#Label-based selection
print(first_df.loc[0, 'B'])
print(first_df.loc[:, ['A', 'B', 'D']])      
      
#Choosing between iloc and loc - 
#iloc uses the Python stdlib indexing scheme,
#where the first element of the range is included and the last one excluded. 
#So 0:10 will select entries 0,...,9. 
#loc, meanwhile, indexes inclusively. So 0:10 will select entries 0,...,10.   
      

      
#Manupulating the index
print(first_df.set_index("D"))  
      
    
      
#Conditional Selection
print(first_df.A == 21)
print(first_df.loc[first_df.A == 21])     
print(first_df.loc[(first_df.A == 21) & (first_df.C ==21)])      
print(first_df.loc[(first_df.A == 21) | (first_df.C ==21)])     
print(first_df.loc[first_df.A.isin([50, 99])]) 
print(first_df.loc[first_df.A.notnull()])      

      
      
#Assigning Data
first_df['A'] = 100
print(first_df['A'])  
      
first_df['B'] = range(len(first_df), 0, -1)
print(first_df['B'])   
      
#-----------------------------------------------------------------------------------------------------------      
            
#Summary functions
      
print(first_df.C.describe())   
      
#To get the `mean`
print(first_df.C.mean())       

#To check the unique values
print(first_df.C.unique())       
      
#To count each values
print(first_df.C.value_counts()) 
      
      
#Maps
#Using map() function      
first_df_C_mean = first_df.C.mean()
print(first_df.map(lambda p: p - first_df_C_mean))   
      
#Using apply() function
def remean_C(row):
      row.C = row.C - first_df_C_mean
      return row    
print(first_df.apply(remean_C, axis = 'columns'))     
      
#----------------------------------------------------------------------------------------------------------------     
      
#Groupwise analysis
#Using value_counts() function
print(first_df.D.value_counts())    
      
#Using min() function
print(first_df.groupby('C').D.min())
      
#using apply() function
print(first_df.groupby('C').apply(lambda df: df.A.iloc[0]))
print(first_df.groupby(['C', 'A']).apply(lambda df: df.loc[df.A.idxmax()]))
      
#Using agg() function
print(first_df.groupby(['C']).A.agg([len, min, max]))   
      
      
#Multi - indexes
first_df_2 = first_df.groupby(['A', 'C']).description.agg([len])
print(first_df_2)  
      

#Sorting
print(first_df_2.sort_values(by='len', ascending=False))
      
#-----------------------------------------------------------------------------------------------------------------      

#Data types      
#Using the function dtype() to check the data-type
print(first_df.dtype)      
print(first_df.C.dtype)
      
#Using the function astype() for conversion
print(first-df.C.astype('float64')      
      

#Missing data
#Using isnull(), notnull() function to check null, not-null values respectively.      
print(first_df[pd.isnull(first_df.C)])
print(first_df[pd.notnull(first_df.C)])  
      
#Using fillna() function to replace null values
print(first_df.A.fillna("Unknown"))      
      
#Using replace() method to repace column values
print(first_df.A.replace(99, 100))      
      
#-----------------------------------------------------------------------------------------------------------------        

#Renaming using rename() function
print(first_df.rename(columns={'C':'F'}))     
   
#Renaming of the indexes      
print(first_df.rename(index={0: 'firstEntry', 1: 'secondEntry'}))

#Combining using concat() function
first_df2 = pd.DataFrame({'A': [12, 33, 14, 89, 22, 296], 
                         'B': [34, 40, 32, 52, 9, 80],
                         'C' : [99, 242, 78, 22, 93, 1], 
                         'D' : [26, 542, 57, 89, 85, 512]}, 
                        index = [6,7,8,9,10,11])      
pd.concat([first_df, first_df2])      
      
#-----------------------------------------------------------------------------------------------------------------        
   
      
      
      
      
     
      
      
      
      
      
      
      


























#Some basic methods and their application in pandas.
#Importing Pandas to use the methods.
import pandas as pd

#---------------------------------------------------------------------------------------------------

#Creating Data files - DataFrames & Series

#To create DataFrames.
first_df = pd.DataFrame({'A': [50, 21, 34, 99, 21, 231], 'B': [131, 2, 342, 563, 90, 88], 'C' : [12, 22, 55, 98, 94, 21], 'D' : [11, 236, 54, 75, 91, 20]})
print(first_df)
second_df = pd.DataFrame({'Bob': ['I liked it.', 'It was awful.'], 'Sue': ['Pretty good.', 'Bland.']}, 'Henry' : ['Not Bad.', 'Poor.'])
print(second_df)

#To create DataFrames using index.
third_df = pd.DataFrame({'Bob': ['I liked it.', 'It was awful.'], 
              'Sue': ['Pretty good.', 'Bland.']},
             index=['Product A', 'Product B'])
print(third_df)


#To create Series.
first_se = pd.Series([1, 2, 3, 4, 5])
print(first_se)

#To create Series using index.
second_se = pd.Series([30, 35, 40], index=['2015 Sales', '2016 Sales', '2017 Sales'], name='Product A')
print(second_se)


#Reading Data files - Comma Separated Values(CSV)
first_df.to_csv("first_df.csv")
print(first_df.shape)
print("\n")
print(first_df.head())

#--------------------------------------------------------------------------------------------------------

#Native accessors
print(first_df)

#To print the A column elements
print(first_df.A)
print(first_df['A']
print(first_df.C)
print(first_df['C']      
      
#To print a specific element of Yes column
print(first_df['A'][0])

   
      
#Indexing
#Index-based selection     
print(first_df.iloc[0])
      
#To get a column with iloc      
print(first_df.iloc[:, 0])  
print(first_df.iloc[:4, 0])      
print(first_df.iloc[1:4, 0])

#To pass a list
print(first_df.iloc[[0, 1, 2], 2])     

#To get last 3 elements
print(first_df.iloc[-3: ])    
      
      
#Label-based selection
print(first_df.loc[0, 'B'])
print(first_df.loc[:, ['A', 'B', 'D']])      
      
#Choosing between iloc and loc - 
#iloc uses the Python stdlib indexing scheme,
#where the first element of the range is included and the last one excluded. 
#So 0:10 will select entries 0,...,9. 
#loc, meanwhile, indexes inclusively. So 0:10 will select entries 0,...,10.   
      

      
#Manupulating the index
print(first_df.set_index("D"))  
      
    
      
#Conditional Selection
print(first_df.A == 21)
print(first_df.loc[first_df.A == 21])     
print(first_df.loc[(first_df.A == 21) & (first_df.C ==21)])      
print(first_df.loc[(first_df.A == 21) | (first_df.C ==21)])     
print(first_df.loc[first_df.A.isin([50, 99])]) 
print(first_df.loc[first_df.A.notnull()])      

      
      
#Assigning Data
first_df['A'] = 100
print(first_df['A'])  
      
first_df['B'] = range(len(first_df), 0, -1)
print(first_df['B'])   
      
#-----------------------------------------------------------------------------------------------------------      
            
#Summary functions
      
print(first_df.C.describe())   
      
#To get the `mean`
print(first_df.C.mean())       

#To check the unique values
print(first_df.C.unique())       
      
#To count each values
print(first_df.C.value_counts()) 
      
      
#Maps
#Using map() function      
first_df_C_mean = first_df.C.mean()
print(first_df.map(lambda p: p - first_df_C_mean))   
      
#Using apply() function
def remean_C(row):
      row.C = row.C - first_df_C_mean
      return row    
print(first_df.apply(remean_C, axis = 'columns'))     
      
#----------------------------------------------------------------------------------------------------------------     
      
#Groupwise analysis
#Using value_counts() function
print(first_df.D.value_counts())    
      
#Using min() function
print(first_df.groupby('C').D.min())
      
#using apply() function
print(first_df.groupby('C').apply(lambda df: df.A.iloc[0]))
print(first_df.groupby(['C', 'A']).apply(lambda df: df.loc[df.A.idxmax()]))
      
#Using agg() function
print(first_df.groupby(['C']).A.agg([len, min, max]))   
      
      
#Multi - indexes
first_df_2 = first_df.groupby(['A', 'C']).description.agg([len])
print(first_df_2)  
      

#Sorting
print(first_df_2.sort_values(by='len', ascending=False))
      
#-----------------------------------------------------------------------------------------------------------------      

#Data types      
#Using the function dtype() to check the data-type
print(first_df.dtype)      
print(first_df.C.dtype)
      
#Using the function astype() for conversion
print(first-df.C.astype('float64')      
      

#Missing data
#Using isnull(), notnull() function to check null, not-null values respectively.      
print(first_df[pd.isnull(first_df.C)])
print(first_df[pd.notnull(first_df.C)])  
      
#Using fillna() function to replace null values
print(first_df.A.fillna("Unknown"))      
      
#Using replace() method to repace column values
print(first_df.A.replace(99, 100))      
      
#-----------------------------------------------------------------------------------------------------------------        

#Renaming using rename() function
print(first_df.rename(columns={'C':'F'}))     
   
#Renaming of the indexes      
print(first_df.rename(index={0: 'firstEntry', 1: 'secondEntry'}))

#Combining using concat() function
first_df2 = pd.DataFrame({'A': [12, 33, 14, 89, 22, 296], 
                         'B': [34, 40, 32, 52, 9, 80],
                         'C' : [99, 242, 78, 22, 93, 1], 
                         'D' : [26, 542, 57, 89, 85, 512]}, 
                        index = [6,7,8,9,10,11])      
pd.concat([first_df, first_df2])      
      
#-----------------------------------------------------------------------------------------------------------------
#Some basic methods and their application in pandas. #Importing Pandas to use the methods. #--------------------------------------------------------------------------------------------------- #Creating Data files - DataFrames & Series #To create DataFrames. #To create DataFrames using index. #To create Series. #To create Series using index. #Reading Data files - Comma Separated Values(CSV) #-------------------------------------------------------------------------------------------------------- #Native accessors #To print the A column elements #To print a specific element of Yes column #Indexing #Index-based selection #To get a column with iloc #To pass a list #To get last 3 elements #Label-based selection #Choosing between iloc and loc - #iloc uses the Python stdlib indexing scheme, #where the first element of the range is included and the last one excluded. #So 0:10 will select entries 0,...,9. #loc, meanwhile, indexes inclusively. So 0:10 will select entries 0,...,10. #Manupulating the index #Conditional Selection #Assigning Data #----------------------------------------------------------------------------------------------------------- #Summary functions #To get the `mean` #To check the unique values #To count each values #Maps #Using map() function #Using apply() function #---------------------------------------------------------------------------------------------------------------- #Groupwise analysis #Using value_counts() function #Using min() function #using apply() function #Using agg() function #Multi - indexes #Sorting #----------------------------------------------------------------------------------------------------------------- #Data types #Using the function dtype() to check the data-type #Using the function astype() for conversion #Missing data #Using isnull(), notnull() function to check null, not-null values respectively. #Using fillna() function to replace null values #Using replace() method to repace column values #----------------------------------------------------------------------------------------------------------------- #Renaming using rename() function #Renaming of the indexes #Combining using concat() function #-----------------------------------------------------------------------------------------------------------------