Monday, May 27, 2019

pandas : Lambda ,Map and Eval Conditions

Lambda ,Map and eval in Pandas


1. Simple 


import pandas as pd

a=pd.DataFrame({"a":["deea","123","deep","deepak"]})
b=pd.DataFrame({"b":["deeap","1234","deepak","deepa"]})
c=pd.DataFrame(map(lambda x,y:x in y,a['a'],b['b']))
print(c)
 
 
result : 
       0
0   True
1   True
2   True
3  False 
 

2. Using Dynamic Formula :

import pandas as pd

a=pd.DataFrame({"a":["deea","123","deep","deepak"]})
b=pd.DataFrame({"b":["deeap","1234","deepak","deepa"]})
c=pd.DataFrame(map(eval(input("enter -lambda x,y:x in y")),a['a'].values,b['b'].values))
print(c)

       0
0   True
1   True
2   True
3  False
 

3)Using Dynamic Formula and 1st columns

import pandas as pd

a=pd.DataFrame({"a":["deea","123","deep","deepak"]})
b=pd.DataFrame({"b":["deeap","1234","deepak","deepa"]})
c=pd.DataFrame(map(eval(input("enter -lambda x,y:x in y")),a.iloc[:,0],b.iloc[:,0]))
print(c)
  
Result: 
  0
0 True
1 True
2 True
3 False 
 
"""

"""

Column Names

data.iloc[:,0] # first column of data frame (first_name)
data.iloc[:,1] # second column of data frame (last_name)
data.iloc[:,-1] # last column of data frame (id)

Rows and Columns
data.iloc[0:5] # first five rows of dataframe
data.iloc[:, 0:2] # first two columns of data frame with all rows
data.iloc[[0,3,6,24], [0,5,6]] # 1st, 4th, 7th, 25th row + 1st 6th 7th columns.
data.iloc[0:5, 5:8] # first 5 rows and 5th, 6th, 7th columns of data frame
"""

No comments:

Post a Comment