Applying methods to multiple datasets in pandas Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Apply a for loop to multiple DataFrames in PandasWhat is the difference between Python's list methods append and extend?How to return multiple values from a function?Understanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?Catch multiple exceptions in one line (except block)Selecting multiple columns in a pandas dataframeRenaming columns in pandas“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandas
Why did Europeans not widely domesticate foxes?
Where did Arya get these scars?
Was Objective-C really a hindrance to Apple software development?
Determinant of a matrix with 2 equal rows
What is the evidence that custom checks in Northern Ireland are going to result in violence?
Will I be more secure with my own router behind my ISP's router?
What's called a person who works as someone who puts products on shelves in stores?
Like totally amazing interchangeable sister outfit accessory swapping or whatever
Co-worker works way more than he should
France's Public Holidays' Puzzle
Why doesn't the university give past final exams' answers?
All ASCII characters with a given bit count
Suing a Police Officer Instead of the Police Department
Why I cannot instantiate a class whose constructor is private in a friend class?
Why does Java have support for time zone offsets with seconds precision?
What do you call an IPA symbol that lacks a name (e.g. ɲ)?
Where can I find how to tex symbols for different fonts?
Will I lose my paid in full property
What were wait-states, and why was it only an issue for PCs?
Page Layouts : 1 column , 2 columns-left , 2 columns-right , 3 column
How to translate "red flag" into Spanish?
Mechanism of the formation of peracetic acid
What was Apollo 13's "Little Jolt" after MECO?
Arriving in Atlanta after US Preclearance in Dublin. Will I go through TSA security in Atlanta to transfer to a connecting flight?
Applying methods to multiple datasets in pandas
Unicorn Meta Zoo #1: Why another podcast?
Announcing the arrival of Valued Associate #679: Cesar Manara
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Apply a for loop to multiple DataFrames in PandasWhat is the difference between Python's list methods append and extend?How to return multiple values from a function?Understanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?Catch multiple exceptions in one line (except block)Selecting multiple columns in a pandas dataframeRenaming columns in pandas“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I would like to use the .assign
method with multiple lambda functions to multiple datasets. So far, I've tried with a for loop without success:
a = pd.DataFrame('a': np.arange(5),
'b': np.arange(5))
b = pd.DataFrame('a': np.arange(5,10),
'b': np.arange(5,10))
for data in [a,b]:
data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
Edit:
The following doesn't work either:
for data in [a,b]:
data = data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
python pandas
add a comment |
I would like to use the .assign
method with multiple lambda functions to multiple datasets. So far, I've tried with a for loop without success:
a = pd.DataFrame('a': np.arange(5),
'b': np.arange(5))
b = pd.DataFrame('a': np.arange(5,10),
'b': np.arange(5,10))
for data in [a,b]:
data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
Edit:
The following doesn't work either:
for data in [a,b]:
data = data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
python pandas
That doesn't work becauseasign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
– cglacet
Mar 22 at 15:10
I guess that in practice you want a solution that works for any number of dataframes?
– cglacet
Mar 22 at 15:13
1
Check out this answer stackoverflow.com/questions/38297292/…
– pistolpete
Mar 22 at 15:16
add a comment |
I would like to use the .assign
method with multiple lambda functions to multiple datasets. So far, I've tried with a for loop without success:
a = pd.DataFrame('a': np.arange(5),
'b': np.arange(5))
b = pd.DataFrame('a': np.arange(5,10),
'b': np.arange(5,10))
for data in [a,b]:
data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
Edit:
The following doesn't work either:
for data in [a,b]:
data = data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
python pandas
I would like to use the .assign
method with multiple lambda functions to multiple datasets. So far, I've tried with a for loop without success:
a = pd.DataFrame('a': np.arange(5),
'b': np.arange(5))
b = pd.DataFrame('a': np.arange(5,10),
'b': np.arange(5,10))
for data in [a,b]:
data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
Edit:
The following doesn't work either:
for data in [a,b]:
data = data.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
python pandas
python pandas
edited Mar 22 at 16:17
jcp
asked Mar 22 at 15:07
jcpjcp
1248
1248
That doesn't work becauseasign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
– cglacet
Mar 22 at 15:10
I guess that in practice you want a solution that works for any number of dataframes?
– cglacet
Mar 22 at 15:13
1
Check out this answer stackoverflow.com/questions/38297292/…
– pistolpete
Mar 22 at 15:16
add a comment |
That doesn't work becauseasign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
– cglacet
Mar 22 at 15:10
I guess that in practice you want a solution that works for any number of dataframes?
– cglacet
Mar 22 at 15:13
1
Check out this answer stackoverflow.com/questions/38297292/…
– pistolpete
Mar 22 at 15:16
That doesn't work because
asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.– cglacet
Mar 22 at 15:10
That doesn't work because
asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.– cglacet
Mar 22 at 15:10
I guess that in practice you want a solution that works for any number of dataframes?
– cglacet
Mar 22 at 15:13
I guess that in practice you want a solution that works for any number of dataframes?
– cglacet
Mar 22 at 15:13
1
1
Check out this answer stackoverflow.com/questions/38297292/…
– pistolpete
Mar 22 at 15:16
Check out this answer stackoverflow.com/questions/38297292/…
– pistolpete
Mar 22 at 15:16
add a comment |
1 Answer
1
active
oldest
votes
The main reason why this doesn't work is that asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
What you want to do is to apply the same function to several objects, that's exactly what the map
function is made for:
def assign(df):
return df.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
(a, b) = map(assign, (a,b))
A more general solution is the following:
# Imagine we don't have control over the following line of code:
dataframes = (a, b)
# We can still use the same solution:
dataframes = tuple(map(assign, dataframes))
print(dataframes[0])
Concerning your edit, the reason why this doesn't work is a bit more interesting. It may not seem obvious in your code, but it will be in this one:
a = [1, 2, 3]
data = a
data = [4, 5, 6]
print(data)
Here there it is clear that this output [4, 5, 6]
and not [1, 2, 3]
.
What happen in both your code and this last one is the same:
data = a
:data
is binded to the same object asa
(resp.b
)data = ...
: creates a new binding, leaving the existing binding ofa
untouched (asdata
was only binded to the same object asa
,data
never wasa
).
In the end, for data in [a, b]:
doesn't mean that data
will be an alias for a
(resp. b
) during the next iteration. (Which is what you may expect when writing this.) Instead for data in [a, b]:
simply is equivalent to:
data = a
# 1st iteration
data = b
# 2nd iteration
thanks! I edited the question because I forgot to put adata = data.assign...
– jcp
Mar 22 at 16:12
1
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
It's clearer now :)
– jcp
Mar 22 at 16:17
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302582%2fapplying-methods-to-multiple-datasets-in-pandas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The main reason why this doesn't work is that asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
What you want to do is to apply the same function to several objects, that's exactly what the map
function is made for:
def assign(df):
return df.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
(a, b) = map(assign, (a,b))
A more general solution is the following:
# Imagine we don't have control over the following line of code:
dataframes = (a, b)
# We can still use the same solution:
dataframes = tuple(map(assign, dataframes))
print(dataframes[0])
Concerning your edit, the reason why this doesn't work is a bit more interesting. It may not seem obvious in your code, but it will be in this one:
a = [1, 2, 3]
data = a
data = [4, 5, 6]
print(data)
Here there it is clear that this output [4, 5, 6]
and not [1, 2, 3]
.
What happen in both your code and this last one is the same:
data = a
:data
is binded to the same object asa
(resp.b
)data = ...
: creates a new binding, leaving the existing binding ofa
untouched (asdata
was only binded to the same object asa
,data
never wasa
).
In the end, for data in [a, b]:
doesn't mean that data
will be an alias for a
(resp. b
) during the next iteration. (Which is what you may expect when writing this.) Instead for data in [a, b]:
simply is equivalent to:
data = a
# 1st iteration
data = b
# 2nd iteration
thanks! I edited the question because I forgot to put adata = data.assign...
– jcp
Mar 22 at 16:12
1
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
It's clearer now :)
– jcp
Mar 22 at 16:17
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
add a comment |
The main reason why this doesn't work is that asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
What you want to do is to apply the same function to several objects, that's exactly what the map
function is made for:
def assign(df):
return df.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
(a, b) = map(assign, (a,b))
A more general solution is the following:
# Imagine we don't have control over the following line of code:
dataframes = (a, b)
# We can still use the same solution:
dataframes = tuple(map(assign, dataframes))
print(dataframes[0])
Concerning your edit, the reason why this doesn't work is a bit more interesting. It may not seem obvious in your code, but it will be in this one:
a = [1, 2, 3]
data = a
data = [4, 5, 6]
print(data)
Here there it is clear that this output [4, 5, 6]
and not [1, 2, 3]
.
What happen in both your code and this last one is the same:
data = a
:data
is binded to the same object asa
(resp.b
)data = ...
: creates a new binding, leaving the existing binding ofa
untouched (asdata
was only binded to the same object asa
,data
never wasa
).
In the end, for data in [a, b]:
doesn't mean that data
will be an alias for a
(resp. b
) during the next iteration. (Which is what you may expect when writing this.) Instead for data in [a, b]:
simply is equivalent to:
data = a
# 1st iteration
data = b
# 2nd iteration
thanks! I edited the question because I forgot to put adata = data.assign...
– jcp
Mar 22 at 16:12
1
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
It's clearer now :)
– jcp
Mar 22 at 16:17
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
add a comment |
The main reason why this doesn't work is that asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
What you want to do is to apply the same function to several objects, that's exactly what the map
function is made for:
def assign(df):
return df.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
(a, b) = map(assign, (a,b))
A more general solution is the following:
# Imagine we don't have control over the following line of code:
dataframes = (a, b)
# We can still use the same solution:
dataframes = tuple(map(assign, dataframes))
print(dataframes[0])
Concerning your edit, the reason why this doesn't work is a bit more interesting. It may not seem obvious in your code, but it will be in this one:
a = [1, 2, 3]
data = a
data = [4, 5, 6]
print(data)
Here there it is clear that this output [4, 5, 6]
and not [1, 2, 3]
.
What happen in both your code and this last one is the same:
data = a
:data
is binded to the same object asa
(resp.b
)data = ...
: creates a new binding, leaving the existing binding ofa
untouched (asdata
was only binded to the same object asa
,data
never wasa
).
In the end, for data in [a, b]:
doesn't mean that data
will be an alias for a
(resp. b
) during the next iteration. (Which is what you may expect when writing this.) Instead for data in [a, b]:
simply is equivalent to:
data = a
# 1st iteration
data = b
# 2nd iteration
The main reason why this doesn't work is that asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.
What you want to do is to apply the same function to several objects, that's exactly what the map
function is made for:
def assign(df):
return df.assign(c = lambda x: x.a+x.b,
d = lambda x: x.a^x.b)
(a, b) = map(assign, (a,b))
A more general solution is the following:
# Imagine we don't have control over the following line of code:
dataframes = (a, b)
# We can still use the same solution:
dataframes = tuple(map(assign, dataframes))
print(dataframes[0])
Concerning your edit, the reason why this doesn't work is a bit more interesting. It may not seem obvious in your code, but it will be in this one:
a = [1, 2, 3]
data = a
data = [4, 5, 6]
print(data)
Here there it is clear that this output [4, 5, 6]
and not [1, 2, 3]
.
What happen in both your code and this last one is the same:
data = a
:data
is binded to the same object asa
(resp.b
)data = ...
: creates a new binding, leaving the existing binding ofa
untouched (asdata
was only binded to the same object asa
,data
never wasa
).
In the end, for data in [a, b]:
doesn't mean that data
will be an alias for a
(resp. b
) during the next iteration. (Which is what you may expect when writing this.) Instead for data in [a, b]:
simply is equivalent to:
data = a
# 1st iteration
data = b
# 2nd iteration
edited Mar 22 at 17:29
answered Mar 22 at 15:16
cglacetcglacet
1,617820
1,617820
thanks! I edited the question because I forgot to put adata = data.assign...
– jcp
Mar 22 at 16:12
1
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
It's clearer now :)
– jcp
Mar 22 at 16:17
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
add a comment |
thanks! I edited the question because I forgot to put adata = data.assign...
– jcp
Mar 22 at 16:12
1
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
It's clearer now :)
– jcp
Mar 22 at 16:17
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
thanks! I edited the question because I forgot to put a
data = data.assign...
– jcp
Mar 22 at 16:12
thanks! I edited the question because I forgot to put a
data = data.assign...
– jcp
Mar 22 at 16:12
1
1
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
You should let your code as it was otherwise people reading the answer and the question won't understand what is going on ^^
– cglacet
Mar 22 at 16:14
It's clearer now :)
– jcp
Mar 22 at 16:17
It's clearer now :)
– jcp
Mar 22 at 16:17
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
I edited too, to have a full answer to this question. I hope it makes sense, let me know if you have any question as I think this is an interesting question/answer to get right and clear.
– cglacet
Mar 22 at 16:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55302582%2fapplying-methods-to-multiple-datasets-in-pandas%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That doesn't work because
asign
doesn't modify the existing dataframe in place, but instead return a new dataframe object.– cglacet
Mar 22 at 15:10
I guess that in practice you want a solution that works for any number of dataframes?
– cglacet
Mar 22 at 15:13
1
Check out this answer stackoverflow.com/questions/38297292/…
– pistolpete
Mar 22 at 15:16