Multiply columns of a dataframe by getting the column names from a list2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?Drop data frame columns by nameSelecting multiple columns in a pandas dataframeUse a list of values to select rows from a pandas dataframeAdding new column to existing DataFrame in Python pandasHow to change the order of DataFrame columns?Delete column from pandas DataFrame by column nameHow do I get the row count of a Pandas dataframe?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
Replacing Windows 7 security updates with anti-virus?
"One can do his homework in the library"
What has been your most complicated TikZ drawing?
Potentiometer like component
Welcoming 2019 Pi day: How to draw the letter π?
How to make readers know that my work has used a hidden constraint?
What to do when during a meeting client people start to fight (even physically) with each others?
US to Europe trip with Montreal layover - is 52 minutes enough?
Is it true that real estate prices mainly go up?
Sword in the Stone story where the sword was held in place by electromagnets
Counter-example to the existence of left Bousfield localization of combinatorial model category
Is having access to past exams cheating and, if yes, could it be proven just by a good grade?
Plywood subfloor won't screw down in a trailer home
If the Captain's screens are out, does he switch seats with the co-pilot?
What is the blue range indicating on this manifold pressure gauge?
Does splitting a potentially monolithic application into several smaller ones help prevent bugs?
Is all copper pipe pretty much the same?
Unreachable code, but reachable with exception
Why would a jet engine that runs at temps excess of 2000°C burn when it crashes?
Extension of Splitting Fields over An Arbitrary Field
The three point beverage
Is it illegal in Germany to take sick leave if you caused your own illness with food?
Confusion with the nameplate of an induction motor
Can "semicircle" be used to refer to a part-circle that is not a exact half-circle?
Multiply columns of a dataframe by getting the column names from a list
2019 Community Moderator ElectionHow to sort a dataframe by multiple column(s)?Drop data frame columns by nameSelecting multiple columns in a pandas dataframeUse a list of values to select rows from a pandas dataframeAdding new column to existing DataFrame in Python pandasHow to change the order of DataFrame columns?Delete column from pandas DataFrame by column nameHow do I get the row count of a Pandas dataframe?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
I have a dataframe in which I have categorical as well as numerical columns.
data = [['A',"India",10,20,30,15,"Cochin"],['B',"India",10,20,30,40,"Chennai"],['C',"India",10,20,30,15,"Chennai"]]
df = pd.DataFrame(data,columns=['Product','Country',"2016 Total","2017 Total","2018 Total","2019 Total","Region"])
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
I know what will be the names of the column of numerical variables(which need to be captured dynamically):
start_year = 2016
current_year = datetime.datetime.now().year
previous_year = current_year - 1
print(current_year)
year_list = np.arange(start_year, current_year+1, 1)
cols_list = []
for i in year_list:
if i <= current_year:
cols = str(i)+" Total"
cols_list.append(cols)
cols_list
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
I am trying to identify if the values in the columns of cols_list when multiplied is negative or not
How this can be done in pandas? I am not able to figure out how to loop through the cols_list and pull the columns from dataframe and multiply
Expected output:
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region Negative
0 A India 10 20 30 15 Cochin No
1 B India 10 20 30 40 Chennai No
2 C India 10 20 30 15 Chennai No
python-3.x pandas dataframe
add a comment |
I have a dataframe in which I have categorical as well as numerical columns.
data = [['A',"India",10,20,30,15,"Cochin"],['B',"India",10,20,30,40,"Chennai"],['C',"India",10,20,30,15,"Chennai"]]
df = pd.DataFrame(data,columns=['Product','Country',"2016 Total","2017 Total","2018 Total","2019 Total","Region"])
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
I know what will be the names of the column of numerical variables(which need to be captured dynamically):
start_year = 2016
current_year = datetime.datetime.now().year
previous_year = current_year - 1
print(current_year)
year_list = np.arange(start_year, current_year+1, 1)
cols_list = []
for i in year_list:
if i <= current_year:
cols = str(i)+" Total"
cols_list.append(cols)
cols_list
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
I am trying to identify if the values in the columns of cols_list when multiplied is negative or not
How this can be done in pandas? I am not able to figure out how to loop through the cols_list and pull the columns from dataframe and multiply
Expected output:
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region Negative
0 A India 10 20 30 15 Cochin No
1 B India 10 20 30 40 Chennai No
2 C India 10 20 30 15 Chennai No
python-3.x pandas dataframe
add a comment |
I have a dataframe in which I have categorical as well as numerical columns.
data = [['A',"India",10,20,30,15,"Cochin"],['B',"India",10,20,30,40,"Chennai"],['C',"India",10,20,30,15,"Chennai"]]
df = pd.DataFrame(data,columns=['Product','Country',"2016 Total","2017 Total","2018 Total","2019 Total","Region"])
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
I know what will be the names of the column of numerical variables(which need to be captured dynamically):
start_year = 2016
current_year = datetime.datetime.now().year
previous_year = current_year - 1
print(current_year)
year_list = np.arange(start_year, current_year+1, 1)
cols_list = []
for i in year_list:
if i <= current_year:
cols = str(i)+" Total"
cols_list.append(cols)
cols_list
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
I am trying to identify if the values in the columns of cols_list when multiplied is negative or not
How this can be done in pandas? I am not able to figure out how to loop through the cols_list and pull the columns from dataframe and multiply
Expected output:
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region Negative
0 A India 10 20 30 15 Cochin No
1 B India 10 20 30 40 Chennai No
2 C India 10 20 30 15 Chennai No
python-3.x pandas dataframe
I have a dataframe in which I have categorical as well as numerical columns.
data = [['A',"India",10,20,30,15,"Cochin"],['B',"India",10,20,30,40,"Chennai"],['C',"India",10,20,30,15,"Chennai"]]
df = pd.DataFrame(data,columns=['Product','Country',"2016 Total","2017 Total","2018 Total","2019 Total","Region"])
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
I know what will be the names of the column of numerical variables(which need to be captured dynamically):
start_year = 2016
current_year = datetime.datetime.now().year
previous_year = current_year - 1
print(current_year)
year_list = np.arange(start_year, current_year+1, 1)
cols_list = []
for i in year_list:
if i <= current_year:
cols = str(i)+" Total"
cols_list.append(cols)
cols_list
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
I am trying to identify if the values in the columns of cols_list when multiplied is negative or not
How this can be done in pandas? I am not able to figure out how to loop through the cols_list and pull the columns from dataframe and multiply
Expected output:
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region Negative
0 A India 10 20 30 15 Cochin No
1 B India 10 20 30 40 Chennai No
2 C India 10 20 30 15 Chennai No
python-3.x pandas dataframe
python-3.x pandas dataframe
asked Mar 7 at 11:07
SamSam
1007
1007
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Use numpy.where
with condition by DataFrame.prod
and Series.lt
for <0
:
#solution with f-strings for get cols_list by year arange
cols_list = [f'x Total' for x in np.arange(start_year, current_year+1)]
print (cols_list)
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
df['Negative'] = np.where(df[cols_list].prod(axis=1).lt(0), 'Yes', 'No')
print (df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
add a comment |
You can use df.filter()
to filter columns having Total
(similar result to your cols_list
) and then use df.prod()
over axis=1
, then s.map()
:
df['Negative']=df.filter(like='Total').prod(axis=1).lt(0).map(True:'Yes',False:'No')
print(df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
add a comment |
Try this:
df['Negative'] = df[cols_list].T.product().apply(lambda x: x < 0)
The df[cols_list].T
there transposes the columns into rows. This way we can take the product
for the rows (which pandas lets us do with a single function call).
Step-by-step:
>>> t = df[cols_list].T
>>> t
0 1 2
2016 10 10 10
2017 20 20 20
2018 30 30 30
>>> p = t.product()
>>> p
0 6000
1 6000
2 6000
dtype: int64
>>> neg = p.apply(lambda x: x < 0)
>>> neg
0 False
1 False
2 False
dtype: bool
New contributor
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%2f55042354%2fmultiply-columns-of-a-dataframe-by-getting-the-column-names-from-a-list%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
Use numpy.where
with condition by DataFrame.prod
and Series.lt
for <0
:
#solution with f-strings for get cols_list by year arange
cols_list = [f'x Total' for x in np.arange(start_year, current_year+1)]
print (cols_list)
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
df['Negative'] = np.where(df[cols_list].prod(axis=1).lt(0), 'Yes', 'No')
print (df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
add a comment |
Use numpy.where
with condition by DataFrame.prod
and Series.lt
for <0
:
#solution with f-strings for get cols_list by year arange
cols_list = [f'x Total' for x in np.arange(start_year, current_year+1)]
print (cols_list)
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
df['Negative'] = np.where(df[cols_list].prod(axis=1).lt(0), 'Yes', 'No')
print (df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
add a comment |
Use numpy.where
with condition by DataFrame.prod
and Series.lt
for <0
:
#solution with f-strings for get cols_list by year arange
cols_list = [f'x Total' for x in np.arange(start_year, current_year+1)]
print (cols_list)
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
df['Negative'] = np.where(df[cols_list].prod(axis=1).lt(0), 'Yes', 'No')
print (df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
Use numpy.where
with condition by DataFrame.prod
and Series.lt
for <0
:
#solution with f-strings for get cols_list by year arange
cols_list = [f'x Total' for x in np.arange(start_year, current_year+1)]
print (cols_list)
['2016 Total', '2017 Total', '2018 Total', '2019 Total']
df['Negative'] = np.where(df[cols_list].prod(axis=1).lt(0), 'Yes', 'No')
print (df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
edited Mar 7 at 11:30
answered Mar 7 at 11:13
jezraeljezrael
346k25302376
346k25302376
add a comment |
add a comment |
You can use df.filter()
to filter columns having Total
(similar result to your cols_list
) and then use df.prod()
over axis=1
, then s.map()
:
df['Negative']=df.filter(like='Total').prod(axis=1).lt(0).map(True:'Yes',False:'No')
print(df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
add a comment |
You can use df.filter()
to filter columns having Total
(similar result to your cols_list
) and then use df.prod()
over axis=1
, then s.map()
:
df['Negative']=df.filter(like='Total').prod(axis=1).lt(0).map(True:'Yes',False:'No')
print(df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
add a comment |
You can use df.filter()
to filter columns having Total
(similar result to your cols_list
) and then use df.prod()
over axis=1
, then s.map()
:
df['Negative']=df.filter(like='Total').prod(axis=1).lt(0).map(True:'Yes',False:'No')
print(df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
You can use df.filter()
to filter columns having Total
(similar result to your cols_list
) and then use df.prod()
over axis=1
, then s.map()
:
df['Negative']=df.filter(like='Total').prod(axis=1).lt(0).map(True:'Yes',False:'No')
print(df)
Product Country 2016 Total 2017 Total 2018 Total 2019 Total Region
0 A India 10 20 30 15 Cochin
1 B India 10 20 30 40 Chennai
2 C India 10 20 30 15 Chennai
Negative
0 No
1 No
2 No
edited Mar 7 at 11:22
answered Mar 7 at 11:10
anky_91anky_91
8,1222721
8,1222721
add a comment |
add a comment |
Try this:
df['Negative'] = df[cols_list].T.product().apply(lambda x: x < 0)
The df[cols_list].T
there transposes the columns into rows. This way we can take the product
for the rows (which pandas lets us do with a single function call).
Step-by-step:
>>> t = df[cols_list].T
>>> t
0 1 2
2016 10 10 10
2017 20 20 20
2018 30 30 30
>>> p = t.product()
>>> p
0 6000
1 6000
2 6000
dtype: int64
>>> neg = p.apply(lambda x: x < 0)
>>> neg
0 False
1 False
2 False
dtype: bool
New contributor
add a comment |
Try this:
df['Negative'] = df[cols_list].T.product().apply(lambda x: x < 0)
The df[cols_list].T
there transposes the columns into rows. This way we can take the product
for the rows (which pandas lets us do with a single function call).
Step-by-step:
>>> t = df[cols_list].T
>>> t
0 1 2
2016 10 10 10
2017 20 20 20
2018 30 30 30
>>> p = t.product()
>>> p
0 6000
1 6000
2 6000
dtype: int64
>>> neg = p.apply(lambda x: x < 0)
>>> neg
0 False
1 False
2 False
dtype: bool
New contributor
add a comment |
Try this:
df['Negative'] = df[cols_list].T.product().apply(lambda x: x < 0)
The df[cols_list].T
there transposes the columns into rows. This way we can take the product
for the rows (which pandas lets us do with a single function call).
Step-by-step:
>>> t = df[cols_list].T
>>> t
0 1 2
2016 10 10 10
2017 20 20 20
2018 30 30 30
>>> p = t.product()
>>> p
0 6000
1 6000
2 6000
dtype: int64
>>> neg = p.apply(lambda x: x < 0)
>>> neg
0 False
1 False
2 False
dtype: bool
New contributor
Try this:
df['Negative'] = df[cols_list].T.product().apply(lambda x: x < 0)
The df[cols_list].T
there transposes the columns into rows. This way we can take the product
for the rows (which pandas lets us do with a single function call).
Step-by-step:
>>> t = df[cols_list].T
>>> t
0 1 2
2016 10 10 10
2017 20 20 20
2018 30 30 30
>>> p = t.product()
>>> p
0 6000
1 6000
2 6000
dtype: int64
>>> neg = p.apply(lambda x: x < 0)
>>> neg
0 False
1 False
2 False
dtype: bool
New contributor
New contributor
answered Mar 7 at 11:19
GBrandtGBrandt
3298
3298
New contributor
New contributor
add a comment |
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%2f55042354%2fmultiply-columns-of-a-dataframe-by-getting-the-column-names-from-a-list%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