Passing a lambda function to DataFrame.apply - what's happening here? [duplicate]












0
















This question already has an answer here:




  • What does “lambda” mean in Python, and what's the simplest way to use it?

    3 answers




My question is about the line



df.apply(lambda x: pd.to_numeric(x, errors='coerce'))


I do understand that this statement converts the dataframe columns to integer values, but was not able to understand the usage of the lambda function or the errors='coerce' part.










share|improve this question















marked as duplicate by jpp pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    To understand error part refer this doc

    – AkshayNevrekar
    Nov 16 '18 at 6:09
















0
















This question already has an answer here:




  • What does “lambda” mean in Python, and what's the simplest way to use it?

    3 answers




My question is about the line



df.apply(lambda x: pd.to_numeric(x, errors='coerce'))


I do understand that this statement converts the dataframe columns to integer values, but was not able to understand the usage of the lambda function or the errors='coerce' part.










share|improve this question















marked as duplicate by jpp pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    To understand error part refer this doc

    – AkshayNevrekar
    Nov 16 '18 at 6:09














0












0








0









This question already has an answer here:




  • What does “lambda” mean in Python, and what's the simplest way to use it?

    3 answers




My question is about the line



df.apply(lambda x: pd.to_numeric(x, errors='coerce'))


I do understand that this statement converts the dataframe columns to integer values, but was not able to understand the usage of the lambda function or the errors='coerce' part.










share|improve this question

















This question already has an answer here:




  • What does “lambda” mean in Python, and what's the simplest way to use it?

    3 answers




My question is about the line



df.apply(lambda x: pd.to_numeric(x, errors='coerce'))


I do understand that this statement converts the dataframe columns to integer values, but was not able to understand the usage of the lambda function or the errors='coerce' part.





This question already has an answer here:




  • What does “lambda” mean in Python, and what's the simplest way to use it?

    3 answers








python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:12









timgeb

51.3k126794




51.3k126794










asked Nov 16 '18 at 6:05









sharma_resharma_re

207




207




marked as duplicate by jpp pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by jpp pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 17 '18 at 20:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    To understand error part refer this doc

    – AkshayNevrekar
    Nov 16 '18 at 6:09














  • 1





    To understand error part refer this doc

    – AkshayNevrekar
    Nov 16 '18 at 6:09








1




1





To understand error part refer this doc

– AkshayNevrekar
Nov 16 '18 at 6:09





To understand error part refer this doc

– AkshayNevrekar
Nov 16 '18 at 6:09












1 Answer
1






active

oldest

votes


















3














apply works on either row- or column-series by applying a function to it. lambda is just defining an anonymous function.



For readability, you could define a regular function with better variable names. Consider the following demo:



>>> df = pd.DataFrame([['1', '2'], ['3', 'foo']])
>>> df.dtypes
>>>
0 object
1 object
dtype: object


We have a dataframe full of strings which we want to make numeric. Non-convertible values should be set to NaN (this is what errors='coerce' does).



>>> def make_numeric(series):
...: return pd.to_numeric(series, errors='coerce')
>>>
>>> new_df = df.apply(make_numeric)
>>>
>>> new_df
>>>
0 1
0 1 2.0
1 3 NaN
>>>
>>> new_df.dtypes
>>>
0 int64
1 float64
dtype: object


As you can see, using the lambda is just a short way of defining a function. If you don't like it, you can always write a normal function that does the same and is probably more readable.



In this case, defining your own function is a little pointless, because you can just write:



>>> df.apply(pd.to_numeric, errors='coerce')
>>>
0 1
0 1 2.0
1 3 NaN





share|improve this answer
























  • (Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

    – timgeb
    Nov 16 '18 at 9:10


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














apply works on either row- or column-series by applying a function to it. lambda is just defining an anonymous function.



For readability, you could define a regular function with better variable names. Consider the following demo:



>>> df = pd.DataFrame([['1', '2'], ['3', 'foo']])
>>> df.dtypes
>>>
0 object
1 object
dtype: object


We have a dataframe full of strings which we want to make numeric. Non-convertible values should be set to NaN (this is what errors='coerce' does).



>>> def make_numeric(series):
...: return pd.to_numeric(series, errors='coerce')
>>>
>>> new_df = df.apply(make_numeric)
>>>
>>> new_df
>>>
0 1
0 1 2.0
1 3 NaN
>>>
>>> new_df.dtypes
>>>
0 int64
1 float64
dtype: object


As you can see, using the lambda is just a short way of defining a function. If you don't like it, you can always write a normal function that does the same and is probably more readable.



In this case, defining your own function is a little pointless, because you can just write:



>>> df.apply(pd.to_numeric, errors='coerce')
>>>
0 1
0 1 2.0
1 3 NaN





share|improve this answer
























  • (Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

    – timgeb
    Nov 16 '18 at 9:10
















3














apply works on either row- or column-series by applying a function to it. lambda is just defining an anonymous function.



For readability, you could define a regular function with better variable names. Consider the following demo:



>>> df = pd.DataFrame([['1', '2'], ['3', 'foo']])
>>> df.dtypes
>>>
0 object
1 object
dtype: object


We have a dataframe full of strings which we want to make numeric. Non-convertible values should be set to NaN (this is what errors='coerce' does).



>>> def make_numeric(series):
...: return pd.to_numeric(series, errors='coerce')
>>>
>>> new_df = df.apply(make_numeric)
>>>
>>> new_df
>>>
0 1
0 1 2.0
1 3 NaN
>>>
>>> new_df.dtypes
>>>
0 int64
1 float64
dtype: object


As you can see, using the lambda is just a short way of defining a function. If you don't like it, you can always write a normal function that does the same and is probably more readable.



In this case, defining your own function is a little pointless, because you can just write:



>>> df.apply(pd.to_numeric, errors='coerce')
>>>
0 1
0 1 2.0
1 3 NaN





share|improve this answer
























  • (Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

    – timgeb
    Nov 16 '18 at 9:10














3












3








3







apply works on either row- or column-series by applying a function to it. lambda is just defining an anonymous function.



For readability, you could define a regular function with better variable names. Consider the following demo:



>>> df = pd.DataFrame([['1', '2'], ['3', 'foo']])
>>> df.dtypes
>>>
0 object
1 object
dtype: object


We have a dataframe full of strings which we want to make numeric. Non-convertible values should be set to NaN (this is what errors='coerce' does).



>>> def make_numeric(series):
...: return pd.to_numeric(series, errors='coerce')
>>>
>>> new_df = df.apply(make_numeric)
>>>
>>> new_df
>>>
0 1
0 1 2.0
1 3 NaN
>>>
>>> new_df.dtypes
>>>
0 int64
1 float64
dtype: object


As you can see, using the lambda is just a short way of defining a function. If you don't like it, you can always write a normal function that does the same and is probably more readable.



In this case, defining your own function is a little pointless, because you can just write:



>>> df.apply(pd.to_numeric, errors='coerce')
>>>
0 1
0 1 2.0
1 3 NaN





share|improve this answer













apply works on either row- or column-series by applying a function to it. lambda is just defining an anonymous function.



For readability, you could define a regular function with better variable names. Consider the following demo:



>>> df = pd.DataFrame([['1', '2'], ['3', 'foo']])
>>> df.dtypes
>>>
0 object
1 object
dtype: object


We have a dataframe full of strings which we want to make numeric. Non-convertible values should be set to NaN (this is what errors='coerce' does).



>>> def make_numeric(series):
...: return pd.to_numeric(series, errors='coerce')
>>>
>>> new_df = df.apply(make_numeric)
>>>
>>> new_df
>>>
0 1
0 1 2.0
1 3 NaN
>>>
>>> new_df.dtypes
>>>
0 int64
1 float64
dtype: object


As you can see, using the lambda is just a short way of defining a function. If you don't like it, you can always write a normal function that does the same and is probably more readable.



In this case, defining your own function is a little pointless, because you can just write:



>>> df.apply(pd.to_numeric, errors='coerce')
>>>
0 1
0 1 2.0
1 3 NaN






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 6:14









timgebtimgeb

51.3k126794




51.3k126794













  • (Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

    – timgeb
    Nov 16 '18 at 9:10



















  • (Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

    – timgeb
    Nov 16 '18 at 9:10

















(Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

– timgeb
Nov 16 '18 at 9:10





(Peculiarly, df.astype has an errors argument, but it can't be 'coerce'.)

– timgeb
Nov 16 '18 at 9:10





Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma