Create a list of columns and sum them in a new column with Pandas (Python)2019 Community Moderator ElectionFinding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?How do I concatenate two lists in Python?Renaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Turning a hard to access nut?

Unfrosted light bulb

Weird lines in Microsoft Word

Do I need an EFI partition for each 18.04 ubuntu I have on my HD?

Exit shell with shortcut (not typing exit) that closes session properly

Why doesn't the fusion process of the sun speed up?

Should I be concerned about student access to a test bank?

How do you justify more code being written by following clean code practices?

What is the difference between something being completely legal and being completely decriminalized?

Would this string work as string?

Error in master's thesis, I do not know what to do

Do I need to convey a moral for each of my blog post?

Why are there no stars visible in cislunar space?

Did Nintendo change its mind about 68000 SNES?

How are passwords stolen from companies if they only store hashes?

Could any one tell what PN is this Chip? Thanks~

Is there any common country to visit for uk and schengen visa?

Is VPN a layer 3 concept?

How to find the largest number(s) in a list of elements, possibly non-unique?

The English Debate

UK Tourist Visa- Enquiry

Output visual diagram of picture

What kind of footwear is suitable for walking in micro gravity environment?

Are hand made posters acceptable in Academia?



Create a list of columns and sum them in a new column with Pandas (Python)



2019 Community Moderator ElectionFinding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?How do I concatenate two lists in Python?Renaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers










3















I do know some posts are quite similar to my question but none of them succeded in giving me the correct answer. I want, for each row of a pandas dataframe, to perform the sum of values taken from several columns. As the number of columns tends to vary, I want this sum to be performed from a list of columns.



At the moment my code looks like this:



df['Sum'] = df['Col A'] + df['Col E'] + df['Col Z']


I want it to be something like :



df['Sum'] = sum(list_of_my_columns)


or



df[list_of_my_columns].sum(axis=1)


But both of them return an error. Might be because my list isn't properly created? This is how I did it:



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


But this doesn't seem to work... Any ideas ? Thank you !










share|improve this question
























  • df[list_of_my_columns].sum(axis=1)

    – Wen-Ben
    Mar 6 at 23:45











  • Thanks, but I've already tried this and it returns an index error :-(

    – virgilus
    Mar 7 at 0:02















3















I do know some posts are quite similar to my question but none of them succeded in giving me the correct answer. I want, for each row of a pandas dataframe, to perform the sum of values taken from several columns. As the number of columns tends to vary, I want this sum to be performed from a list of columns.



At the moment my code looks like this:



df['Sum'] = df['Col A'] + df['Col E'] + df['Col Z']


I want it to be something like :



df['Sum'] = sum(list_of_my_columns)


or



df[list_of_my_columns].sum(axis=1)


But both of them return an error. Might be because my list isn't properly created? This is how I did it:



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


But this doesn't seem to work... Any ideas ? Thank you !










share|improve this question
























  • df[list_of_my_columns].sum(axis=1)

    – Wen-Ben
    Mar 6 at 23:45











  • Thanks, but I've already tried this and it returns an index error :-(

    – virgilus
    Mar 7 at 0:02













3












3








3








I do know some posts are quite similar to my question but none of them succeded in giving me the correct answer. I want, for each row of a pandas dataframe, to perform the sum of values taken from several columns. As the number of columns tends to vary, I want this sum to be performed from a list of columns.



At the moment my code looks like this:



df['Sum'] = df['Col A'] + df['Col E'] + df['Col Z']


I want it to be something like :



df['Sum'] = sum(list_of_my_columns)


or



df[list_of_my_columns].sum(axis=1)


But both of them return an error. Might be because my list isn't properly created? This is how I did it:



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


But this doesn't seem to work... Any ideas ? Thank you !










share|improve this question
















I do know some posts are quite similar to my question but none of them succeded in giving me the correct answer. I want, for each row of a pandas dataframe, to perform the sum of values taken from several columns. As the number of columns tends to vary, I want this sum to be performed from a list of columns.



At the moment my code looks like this:



df['Sum'] = df['Col A'] + df['Col E'] + df['Col Z']


I want it to be something like :



df['Sum'] = sum(list_of_my_columns)


or



df[list_of_my_columns].sum(axis=1)


But both of them return an error. Might be because my list isn't properly created? This is how I did it:



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


But this doesn't seem to work... Any ideas ? Thank you !







python python-3.x pandas jupyter-notebook jupyter-lab






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 15:59







virgilus

















asked Mar 6 at 23:37









virgilusvirgilus

184




184












  • df[list_of_my_columns].sum(axis=1)

    – Wen-Ben
    Mar 6 at 23:45











  • Thanks, but I've already tried this and it returns an index error :-(

    – virgilus
    Mar 7 at 0:02

















  • df[list_of_my_columns].sum(axis=1)

    – Wen-Ben
    Mar 6 at 23:45











  • Thanks, but I've already tried this and it returns an index error :-(

    – virgilus
    Mar 7 at 0:02
















df[list_of_my_columns].sum(axis=1)

– Wen-Ben
Mar 6 at 23:45





df[list_of_my_columns].sum(axis=1)

– Wen-Ben
Mar 6 at 23:45













Thanks, but I've already tried this and it returns an index error :-(

– virgilus
Mar 7 at 0:02





Thanks, but I've already tried this and it returns an index error :-(

– virgilus
Mar 7 at 0:02












3 Answers
3






active

oldest

votes


















1














You do not need the list of columns. You need a list of column names:



list_of_my_columns = ['Col A', 'Col E', 'Col Z']
df['Sum'] = df[list_of_my_columns].sum(axis=1)





share|improve this answer


















  • 1





    Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

    – virgilus
    Mar 7 at 0:26


















2














Let me write as an answer



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


Using concat



df['Sum']=pd.concat(list_of_my_columns,axis=1 ).sum(axis=1)





share|improve this answer























  • Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

    – virgilus
    Mar 7 at 0:14



















0














All you have to do is create a Series (which is a column in pandas) and take the sum of each row and append it to the Series. Then just add the Series to the dataframe.



import pandas as pd

df = pd.read_csv('input.csv', header=None)
col = pd.Series()

for i, row in df.iterrows():
col.set_value(i, (row[0] + row[1] + row[2]))

df = df.assign(sum=col)

print(df)





share|improve this answer























  • When you use a loop by rows in pandas, you are almost surely doing it wrong.

    – DYZ
    Mar 7 at 0:12










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%2f55033883%2fcreate-a-list-of-columns-and-sum-them-in-a-new-column-with-pandas-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You do not need the list of columns. You need a list of column names:



list_of_my_columns = ['Col A', 'Col E', 'Col Z']
df['Sum'] = df[list_of_my_columns].sum(axis=1)





share|improve this answer


















  • 1





    Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

    – virgilus
    Mar 7 at 0:26















1














You do not need the list of columns. You need a list of column names:



list_of_my_columns = ['Col A', 'Col E', 'Col Z']
df['Sum'] = df[list_of_my_columns].sum(axis=1)





share|improve this answer


















  • 1





    Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

    – virgilus
    Mar 7 at 0:26













1












1








1







You do not need the list of columns. You need a list of column names:



list_of_my_columns = ['Col A', 'Col E', 'Col Z']
df['Sum'] = df[list_of_my_columns].sum(axis=1)





share|improve this answer













You do not need the list of columns. You need a list of column names:



list_of_my_columns = ['Col A', 'Col E', 'Col Z']
df['Sum'] = df[list_of_my_columns].sum(axis=1)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 0:10









DYZDYZ

27.6k62150




27.6k62150







  • 1





    Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

    – virgilus
    Mar 7 at 0:26












  • 1





    Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

    – virgilus
    Mar 7 at 0:26







1




1





Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

– virgilus
Mar 7 at 0:26





Thanks! You rightly pointed out my mistake! Using the column names is much more efficient than creating a new df. :)

– virgilus
Mar 7 at 0:26













2














Let me write as an answer



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


Using concat



df['Sum']=pd.concat(list_of_my_columns,axis=1 ).sum(axis=1)





share|improve this answer























  • Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

    – virgilus
    Mar 7 at 0:14
















2














Let me write as an answer



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


Using concat



df['Sum']=pd.concat(list_of_my_columns,axis=1 ).sum(axis=1)





share|improve this answer























  • Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

    – virgilus
    Mar 7 at 0:14














2












2








2







Let me write as an answer



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


Using concat



df['Sum']=pd.concat(list_of_my_columns,axis=1 ).sum(axis=1)





share|improve this answer













Let me write as an answer



list_of_my_columns = [df['Col A'], df['Col E'], df['Col Z']]


Using concat



df['Sum']=pd.concat(list_of_my_columns,axis=1 ).sum(axis=1)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 0:03









Wen-BenWen-Ben

118k83469




118k83469












  • Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

    – virgilus
    Mar 7 at 0:14


















  • Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

    – virgilus
    Mar 7 at 0:14

















Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

– virgilus
Mar 7 at 0:14






Works like a charm thank you! So you used the pd.concat function to create a temporary dataframe containing only my columns, right? Two questions : - When I create a list of columns, are they duplicated in the variable meaning they take more space in the RAM? - Same thing with the df generated with pd.concat, does the new dataframe takes up memory space as well? Or is it destroyed right after the sum has been computed?

– virgilus
Mar 7 at 0:14












0














All you have to do is create a Series (which is a column in pandas) and take the sum of each row and append it to the Series. Then just add the Series to the dataframe.



import pandas as pd

df = pd.read_csv('input.csv', header=None)
col = pd.Series()

for i, row in df.iterrows():
col.set_value(i, (row[0] + row[1] + row[2]))

df = df.assign(sum=col)

print(df)





share|improve this answer























  • When you use a loop by rows in pandas, you are almost surely doing it wrong.

    – DYZ
    Mar 7 at 0:12















0














All you have to do is create a Series (which is a column in pandas) and take the sum of each row and append it to the Series. Then just add the Series to the dataframe.



import pandas as pd

df = pd.read_csv('input.csv', header=None)
col = pd.Series()

for i, row in df.iterrows():
col.set_value(i, (row[0] + row[1] + row[2]))

df = df.assign(sum=col)

print(df)





share|improve this answer























  • When you use a loop by rows in pandas, you are almost surely doing it wrong.

    – DYZ
    Mar 7 at 0:12













0












0








0







All you have to do is create a Series (which is a column in pandas) and take the sum of each row and append it to the Series. Then just add the Series to the dataframe.



import pandas as pd

df = pd.read_csv('input.csv', header=None)
col = pd.Series()

for i, row in df.iterrows():
col.set_value(i, (row[0] + row[1] + row[2]))

df = df.assign(sum=col)

print(df)





share|improve this answer













All you have to do is create a Series (which is a column in pandas) and take the sum of each row and append it to the Series. Then just add the Series to the dataframe.



import pandas as pd

df = pd.read_csv('input.csv', header=None)
col = pd.Series()

for i, row in df.iterrows():
col.set_value(i, (row[0] + row[1] + row[2]))

df = df.assign(sum=col)

print(df)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 23:58









SafeDevSafeDev

15717




15717












  • When you use a loop by rows in pandas, you are almost surely doing it wrong.

    – DYZ
    Mar 7 at 0:12

















  • When you use a loop by rows in pandas, you are almost surely doing it wrong.

    – DYZ
    Mar 7 at 0:12
















When you use a loop by rows in pandas, you are almost surely doing it wrong.

– DYZ
Mar 7 at 0:12





When you use a loop by rows in pandas, you are almost surely doing it wrong.

– DYZ
Mar 7 at 0:12

















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%2f55033883%2fcreate-a-list-of-columns-and-sum-them-in-a-new-column-with-pandas-python%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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved