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;








1















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)









share|improve this question
























  • 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

















1















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)









share|improve this question
























  • 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













1












1








1








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)









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 16:17







jcp

















asked Mar 22 at 15:07









jcpjcp

1248




1248












  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















1














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:




  1. data = a: data is binded to the same object as a (resp. b)


  2. data = ...: creates a new binding, leaving the existing binding of a untouched (as data was only binded to the same object as a, data never was a).

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





share|improve this answer

























  • thanks! I edited the question because I forgot to put a data = 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












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
);



);













draft saved

draft discarded


















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









1














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:




  1. data = a: data is binded to the same object as a (resp. b)


  2. data = ...: creates a new binding, leaving the existing binding of a untouched (as data was only binded to the same object as a, data never was a).

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





share|improve this answer

























  • thanks! I edited the question because I forgot to put a data = 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
















1














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:




  1. data = a: data is binded to the same object as a (resp. b)


  2. data = ...: creates a new binding, leaving the existing binding of a untouched (as data was only binded to the same object as a, data never was a).

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





share|improve this answer

























  • thanks! I edited the question because I forgot to put a data = 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














1












1








1







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:




  1. data = a: data is binded to the same object as a (resp. b)


  2. data = ...: creates a new binding, leaving the existing binding of a untouched (as data was only binded to the same object as a, data never was a).

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





share|improve this answer















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:




  1. data = a: data is binded to the same object as a (resp. b)


  2. data = ...: creates a new binding, leaving the existing binding of a untouched (as data was only binded to the same object as a, data never was a).

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






share|improve this answer














share|improve this answer



share|improve this answer








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 a data = 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






  • 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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현