Pandas Tutorials

Practical Implementation of Pandas DataFrame Attributes 0

Practical Implementation of Pandas DataFrame Attributes

Program 1 import pandas as pd mydata=[(101,’Vishal’,9000),(102,’Amit’,7000),(103,’Rajesh’,8000),(104,’Vinit’,8000)] df=pd.DataFrame(mydata,index=[‘a”b”c”d’],columns=[‘A’,’B’,’C’]) #print(df.size) #print(df.ndim) #print(df.shape) #print(df.values) #print(df.memory_usage())  

How to Insert, Delete, Update in Pandas DataFrames 2

How to Insert, Delete, Update in Pandas DataFrames

Program 1 import pandas as pd emp=pd.read_excel(“E:\mypandas\employee.xlsx”) #print(emp) #emp=emp._append({‘id’:36,’empname’:’Rohit Sharma’,’empdept’:’CS’,’gender’:’M’,’age’:39,’HRA’:5000,’TA’:7000,’DA’:8000,’salary’:20000},ignore_index=True) # print(emp) # print(“—————After Update———————-“) # emp.loc[emp[‘salary’]==15000,’salary’]=25000 # print(emp[emp.salary==25000]) #print(emp) emp=emp.drop(emp[emp.salary==10000].index) print(emp) # _append() # loc() # drop()  

How to Use isin() and not isin() Method in Pandas DataFrame 0

How to Use isin() and not isin() Method in Pandas DataFrame

Program 1 import pandas as pd emp=pd.read_excel(“E:\mypandas\employee.xlsx”) #select * from emp where salary in(5000,6000,8000); #print(emp[emp.salary.isin([6000,7000])]) #print(emp[emp.salary.isin([6000,7000,10000])]) #print(emp[emp.age.isin([30,21,50])]) #select * from employee where salary not in(5000,6000,8000); #print(emp[~emp.salary.isin([6000,7000,10000])]) print(emp[(~emp.age.isin([30,21,40,25,45])) & (emp.salary>10000)])  

Practical Implementation of Pandas DataFrame nlargest() Method 0

Practical Implementation of Pandas DataFrame nlargest() Method

Program 1 import pandas as pd emp=pd.read_excel(“E:\mypandas\employee.xlsx”) #print(emp) #print(emp.nlargest(10,columns=’salary’)) #select * from employee order by salary desc limit 4; #print(emp.nlargest(8,columns=’salary’).tail(4)) #print(emp.nlargest(10,columns=’HRA’).head(5)) #print(emp.nlargest(10,columns=’HRA’).tail(5))    

Practical Implementation of Pandas Groupby Function 0

Practical Implementation of Pandas Groupby Function

Program 1 import pandas as pd product=[(‘Limca’,25,’BK’),(‘Limca’,40,’NM’),(‘Frooti’,20,’AK’), (‘BMilk’,20,’Amul’),(‘Milk’,20,’Amul’),(‘BMilk’,18,’Sanchi’), (‘Limca’,15,’AK’),(‘Frooti’,17,’BK’),(‘BMilk’,23,’Sanchi’), (‘Milk’,27,’NK’),(‘Frooti’,23,’BK’),(‘BMilk’,23,’Sanchi’), ] df=pd.DataFrame(product,columns=[‘Product_Name’,’Proudct_Price’,’Distributor’]) df1=df.groupby(‘Product_Name’) # for name,rows in df1: # print(name) # print(rows) #print(df1.get_group(‘BMilk’).min()) # print(df1.count()) print(df1.agg())  

Practical Implementation of Aggregate Functions in Pandas 0

Practical Implementation of Aggregate Functions in Pandas

Program 1 import pandas as pd product=[(‘Limca’,25,’BK’),(‘Limca’,40,’NM’),(‘Frooti’,20,’AK’), (‘BMilk’,20,’Amul’),(‘Milk’,20,’Amul’),(‘BMilk’,18,’Sanchi’), (‘Limca’,15,’AK’),(‘Frooti’,17,’BK’),(‘BMilk’,23,’Sanchi’), (‘Milk’,27,’NK’),(‘Frooti’,23,’BK’),(‘BMilk’,23,’Sanchi’), ] df=pd.DataFrame(product,columns=[‘Name’,’Price’,’Distb’]) #print(df) df1=df.groupby(‘Name’) # for name,rows in df1: # print(name) # print(rows) print(df1[‘Price’].agg([‘max’,’min’,’mean’,’count’]))  

How to Use where() in Python Pandas 0

How to Use where() in Python Pandas

Program 1 import pandas as pd df=pd.read_excel(“E:\mypandas\mydata.xlsx”,sheet_name=’Result’) print(df) print(“—————After Where————–“) df.set_index(‘Name’,inplace=True) #print(df.where(df<75,’First’)) #print(df.where(df[‘HSC’]<75,’First’)) df1=df.where(lambda x: x < 80, ‘A+’) print(df1)  

Practical Implementation of Pandas Concatenation 0

Practical Implementation of Pandas Concatenation

Program 1 import pandas as pd df1=pd.DataFrame({’empname’:[‘Vivek’,’Dipesh’,’Rajesh’],’Age’:[45,44,36]}) df2=pd.DataFrame({’empname’:[‘Vivek’,’Dipesh’,’Vishal’],’Salary(in Lakhs)’:[23,16,10]}) df3=pd.concat([df1,df2],ignore_index=True) print(df3)

How to Apply Join Without a Common Column in Pandas 0

How to Apply Join Without a Common Column in Pandas

Program 1 import pandas as pd df1=pd.DataFrame({’empname’:[‘Vivek’,’Rahul’,’Nilesh’],’Age’:[43,38,27]}) df2=pd.DataFrame({‘citizen’:[‘Vivek’,’Rahul’,’Anurag’],’Salary(In Lakhs)’:[20,16,19]}) #Inner Join print(“Inner Join”) print(pd.merge(df1,df2,left_on=’empname’,right_on=’citizen’,how=’inner’)) #left Join print(“Left Join”) print(pd.merge(df1,df2,left_on=’empname’,right_on=’citizen’,how=’left’)) #Right join print(“Right Join”) print(pd.merge(df1,df2,left_on=’empname’,right_on=’citizen’,how=’right’)) #Outer Join print(“Outer Join”) print(pd.merge(df1,df2,left_on=’empname’,right_on=’citizen’,how=’outer’))  

Practical Implementation of Joins in Python Pandas 0

Practical Implementation of Joins in Python Pandas

Program 1 import pandas as pd df1=pd.DataFrame({’empname’:[‘Vivek’,’Rahul’,’Amit’],’Age’:[42,37,27]}) df2=pd.DataFrame({’empname’:[‘Vivek’,’Rahul’,’Ravi’],’Salary(in Lakhs)’:[20,29,15]}) #inner join print(“Result of Inner Join”) print(pd.merge(df1,df2,on=’empname’,how=’inner’)) #left join print(“Result of Left Join”) print(pd.merge(df1,df2,on=’empname’,how=’left’)) #right join print(“Result of Right Join”) print(pd.merge(df1,df2,on=’empname’,how=’right’)) #full join print(“Result of...