in Education by
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) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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 as a (resp. b) 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

Related questions

0 votes
    Working with census data, I want to replace NaNs in two columns ("workclass" and "native-country") with the ... way to do this? Select the correct answer from above options...
asked Feb 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Pandas Merging 101 (6 answers) Closed 3 years ago. Sorry guys, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    In [60]: print(row.index) Int64Index([15], dtype='int64') I already know that the row number is ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    Say I have a DataFrame full of positive samples and context features for a given user: target user cashtag ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I'm having an issue currently with pulling from an API, getting a JSON dict, then flattening it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm having an issue currently with pulling from an API, getting a JSON dict, then flattening it, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    My data looks as follows: ID my_val db_val a X X a X X a Y X b X Y b Y Y b ... JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    How can I remove emojis that start with '\x' when reading a csv file using pandas in Python? The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    0 2 ['name:', 'Atlanta', 'GA:', 'Hartsfield-Jackson', 'Atlanta', 'International'] 35 ['name: ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I am trying to groupby a column and compute value counts on another column. import pandas as pd dftest = pd. ... Amt, already exists Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I'm starting from the pandas DataFrame docs here: http://pandas.pydata.org/pandas-docs/stable/dsintro.html I'd ... =1)] print valdict Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    How do I get the index column name in python pandas? Here's an example dataframe: Index Title Column 1 Apples 1 ... how to do this? Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
...