I wanted to try out the functionality of applymap
method of Pandas DataFrame
object. Here is the Use case:
Let say my DataFrame df1
is as follows:
Age ID Name
0 27 101 John
1 22 102 Bob
2 19 103 Alok
3 27 104 Tom
4 32 105 Matt
5 19 106 Steve
6 5 107 Tom
7 55 108 Dick
8 67 109 Harry
Now I want to create a flag variable with the logic that if length of element is less than 2, then flag=1 else flag=0.
In order to run this element-wise, I wanted to use applymap
method. So for that I created a user defined function as follows:
def f(x):
if len(str(x))>2:
df1['Flag']=1
else:
df1['Flag']=0
Then I ran df1.applymap(f)
which gave:
Age ID Name
0 None None None
1 None None None
2 None None None
3 None None None
4 None None None
5 None None None
6 None None None
7 None None None
8 None None None
instead of creating a flag variable with the flag value. How can I achieve the desired functionality using applymap
?
Can't we use the DataFrame variable name or pandas statement inside the user defined function? I.e., is df1['Flag']
valid inside the definition of f()
?