Applying lambda on Python DataFrame
Applying lambda on Python DataFrame
I am having a Python Pandas DataFrame like
>>> df
classification like
0 flower 1
1 flower 0
2 flower 0
3 adventure 1
4 adventure 1
I want to create an output DataFrame like
>>> df
classification like liked
0 flower 1 True
1 flower 0 False
2 flower 0 False
3 adventure 1 True
4 adventure 1 True
I am "apply"ing the Python lambda function on the input DataFrame as follows:
>>> df['like'].apply(lambda x: x == 1)
But I am getting all 'False' under the 'liked' column
>>> df
classification like liked
0 flower 1 False
1 flower 0 False
2 flower 0 False
3 adventure 1 False
4 adventure 1 False
Any quick suggestions will be helpful.
>>> df['like'].astype(int)
0 1
1 0
2 0
3 1
4 1
Name: like, dtype: int32
@jezrael
>>> df['liked'] = df['like'].astype(bool)
>>> df
classification like liked
0 flower 1 True
1 flower 0 True
2 flower 0 True
3 adventure 1 True
4 adventure 1 True
@jezrael : DTypes
>>> df.dtypes
classification object
like object
liked bool
dtype: object
df['like'].astype(int)
If you can avoid
apply
which is a hidden loop as your needs can be vectorized.– Parfait
Jun 29 at 13:14
apply
1 Answer
1
Convert integer column to boolean:
print (type(df.loc[0, 'like']))
<class 'numpy.int64'>
df['liked'] = df['like'].astype(bool)
Or assign comparison with 1:
df['liked'] = df['like'] == 1
If 1
is string compare by string '1'
:
1
'1'
print (type(df.loc[0, 'like']))
<class 'str'>
df['liked'] = df['like'] == '1'
print (df)
classification like liked
0 flower 1 True
1 flower 0 False
2 flower 0 False
3 adventure 1 True
4 adventure 1 True
When I compared using df['like'] == '1' it is working. But the problem is when I checked the data type for the 'like' column it is showing as int32
– somnathchakrabarti
Jun 29 at 13:24
Do you check it by
print (type(df.loc[0, 'like']))
?– jezrael
Jun 29 at 13:25
print (type(df.loc[0, 'like']))
No I checked by df['like'].astype(int)
– somnathchakrabarti
Jun 29 at 13:26
Here is possible check it by
print (df.dtypes)
, it return object
for string column– jezrael
Jun 29 at 13:27
print (df.dtypes)
object
ok, so it is string that's the problem
– somnathchakrabarti
Jun 29 at 13:29
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
try
df['like'].astype(int)
– Rakesh
Jun 29 at 13:13