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
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
add a comment |
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
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
add a comment |
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
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
python python-3.x pandas jupyter-notebook jupyter-lab
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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)
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
add a comment |
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)
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
add a comment |
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)
When you use a loop by rows in pandas, you are almost surely doing it wrong.
– DYZ
Mar 7 at 0:12
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%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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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)
When you use a loop by rows in pandas, you are almost surely doing it wrong.
– DYZ
Mar 7 at 0:12
add a comment |
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)
When you use a loop by rows in pandas, you are almost surely doing it wrong.
– DYZ
Mar 7 at 0:12
add a comment |
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)
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)
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
add a comment |
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
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%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
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
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