I see
- df["col2"] = df["col1"].apply(len)
- len(df["col1"])
My question is,
why use "len" function without parenthesis in 1, but use it with parenthesis in 2?
what is the difference between the two?
I see this kind of occasion a lot, where using a function with and without parenthesis.
Can someone explain me what exactly is going on?
Thanks.
>Solution :
The first example that you mentioned(the above code) maps the function len to the target variable df["col1"]
df["col2"] = df["col1"].apply(len)
Whenever we have to map a function to any iterable object, the syntax needs the function to be without parenthesis.
In your case, df["col1"] must be having elements whose length can be calculated. And it will return a Pandas Series will lengths of all the elements.
Take the following example.
a = ["1", "2","3","4"]
z = list( map( int, a ) ) >> [1, 2, 3, 4]
Here, we mapped the builtin int function(which does typecasting), to the entire list.
The second example that you mentioned would give out the length of the df["col1"] series.
len(df["col1"])
It won’t do any operations on the elements within that Series.
Take the following example.
a = ["1", "2","3","4"]
z = len(a) >> 4
Since, on both the occasions, the function len was fed an iterable object, it didn’t give any error. But, the outputs are completely different as I explained!