Replace 1s in one hot columns with values from another column











up vote
8
down vote

favorite












I have a data frame that looks like this:



df = pd.DataFrame({"value": [4, 5, 3], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]})
df

value item1 item2 item3
0 4 0 1 0
1 5 1 0 0
2 3 0 0 1


Basically what I want to do is replace the value of the one hot encoded elements with the value from the "value" column and then delete the "value" column. The resulting data frame should be like this:



df_out = pd.DataFrame({"item1": [0, 5, 0], "item2": [4, 0, 0], "item3": [0, 0, 3]})

item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3









share|improve this question









New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • i think this can be solved if you just use df["columNameToReplace"] = df["value"] and then delete the value from the dataframe ?
    – Vaibhav gusain
    Dec 5 at 12:47

















up vote
8
down vote

favorite












I have a data frame that looks like this:



df = pd.DataFrame({"value": [4, 5, 3], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]})
df

value item1 item2 item3
0 4 0 1 0
1 5 1 0 0
2 3 0 0 1


Basically what I want to do is replace the value of the one hot encoded elements with the value from the "value" column and then delete the "value" column. The resulting data frame should be like this:



df_out = pd.DataFrame({"item1": [0, 5, 0], "item2": [4, 0, 0], "item3": [0, 0, 3]})

item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3









share|improve this question









New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • i think this can be solved if you just use df["columNameToReplace"] = df["value"] and then delete the value from the dataframe ?
    – Vaibhav gusain
    Dec 5 at 12:47















up vote
8
down vote

favorite









up vote
8
down vote

favorite











I have a data frame that looks like this:



df = pd.DataFrame({"value": [4, 5, 3], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]})
df

value item1 item2 item3
0 4 0 1 0
1 5 1 0 0
2 3 0 0 1


Basically what I want to do is replace the value of the one hot encoded elements with the value from the "value" column and then delete the "value" column. The resulting data frame should be like this:



df_out = pd.DataFrame({"item1": [0, 5, 0], "item2": [4, 0, 0], "item3": [0, 0, 3]})

item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3









share|improve this question









New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a data frame that looks like this:



df = pd.DataFrame({"value": [4, 5, 3], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]})
df

value item1 item2 item3
0 4 0 1 0
1 5 1 0 0
2 3 0 0 1


Basically what I want to do is replace the value of the one hot encoded elements with the value from the "value" column and then delete the "value" column. The resulting data frame should be like this:



df_out = pd.DataFrame({"item1": [0, 5, 0], "item2": [4, 0, 0], "item3": [0, 0, 3]})

item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3






python pandas dataframe






share|improve this question









New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Dec 5 at 12:55









coldspeed

113k18104176




113k18104176






New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 5 at 12:41









Gorjan Radevski

434




434




New contributor




Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Gorjan Radevski is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • i think this can be solved if you just use df["columNameToReplace"] = df["value"] and then delete the value from the dataframe ?
    – Vaibhav gusain
    Dec 5 at 12:47




















  • i think this can be solved if you just use df["columNameToReplace"] = df["value"] and then delete the value from the dataframe ?
    – Vaibhav gusain
    Dec 5 at 12:47


















i think this can be solved if you just use df["columNameToReplace"] = df["value"] and then delete the value from the dataframe ?
– Vaibhav gusain
Dec 5 at 12:47






i think this can be solved if you just use df["columNameToReplace"] = df["value"] and then delete the value from the dataframe ?
– Vaibhav gusain
Dec 5 at 12:47














4 Answers
4






active

oldest

votes

















up vote
12
down vote



accepted










Why not just multiply?



df.pop('value').values * df

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3


DataFrame.pop has the nice effect of in-place removing and returning a column, so you can do this in a single step.





if the "item_*" columns have anything besides 1 in them, then you can multiply with bools:



df.pop('value').values * df.astype(bool)

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3




If your DataFrame has other columns, then do this:



df
value name item1 item2 item3
0 4 John 0 1 0
1 5 Mike 1 0 0
2 3 Stan 0 0 1

# cols = df.columns[df.columns.str.startswith('item')]
cols = df.filter(like='item').columns
df[cols] = df.pop('value').values * df[cols]

df
name item1 item2 item3
0 John 0 5 0
1 Mike 4 0 0
2 Stan 0 0 3





share|improve this answer



















  • 4




    Most elegant answer so far
    – horro
    Dec 5 at 12:53










  • I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
    – Gorjan Radevski
    Dec 5 at 13:08












  • @GorjanRadevski Let me know if the edit does it for you.
    – coldspeed
    Dec 5 at 13:11






  • 1




    I feel so stupid after watching this answer :}
    – Mohit Motwani
    Dec 5 at 13:28






  • 1




    That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
    – Gorjan Radevski
    Dec 5 at 13:46


















up vote
1
down vote













You could do something like:



df = pd.DataFrame([df['value']*df['item1'],df['value']*df['item2'],df['value']*df['item3']])
df.columns = ['item1','item2','item3']


EDIT:
As this answer will not scale well to many columns as @coldspeed comments, it should be done iterating a loop:



 cols = ['item1','item2','item3']
for c in cols:
df[c] *= df['value']
df.drop('value',axis=1,inplace=True)





share|improve this answer



















  • 1




    This won't scale well to many columns.
    – coldspeed
    Dec 5 at 12:52






  • 1




    Fair point, it should be done iterating a loop
    – horro
    Dec 5 at 12:53


















up vote
0
down vote













You need:



col = ['item1','item2','item3']

for c in col:
df[c] = df[c] * df['value']

df.drop(['value'],1,inplace=True)





share|improve this answer



















  • 1




    Surely you can think of something better than iteration...
    – coldspeed
    Dec 5 at 12:52


















up vote
0
down vote













You can use np.where:



items = ['item1', 'item2', 'item3']
for item in items:
df[item] = np.where(df[item]==1, df['value'], 0)
df.drop(columns = ['value'], inplace =True)
df
item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3





share|improve this answer



















  • 1




    Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
    – coldspeed
    Dec 5 at 12:53










  • @coldspeed You're right
    – Mohit Motwani
    Dec 5 at 12:56











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',
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
});


}
});






Gorjan Radevski is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53632617%2freplace-1s-in-one-hot-columns-with-values-from-another-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
12
down vote



accepted










Why not just multiply?



df.pop('value').values * df

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3


DataFrame.pop has the nice effect of in-place removing and returning a column, so you can do this in a single step.





if the "item_*" columns have anything besides 1 in them, then you can multiply with bools:



df.pop('value').values * df.astype(bool)

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3




If your DataFrame has other columns, then do this:



df
value name item1 item2 item3
0 4 John 0 1 0
1 5 Mike 1 0 0
2 3 Stan 0 0 1

# cols = df.columns[df.columns.str.startswith('item')]
cols = df.filter(like='item').columns
df[cols] = df.pop('value').values * df[cols]

df
name item1 item2 item3
0 John 0 5 0
1 Mike 4 0 0
2 Stan 0 0 3





share|improve this answer



















  • 4




    Most elegant answer so far
    – horro
    Dec 5 at 12:53










  • I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
    – Gorjan Radevski
    Dec 5 at 13:08












  • @GorjanRadevski Let me know if the edit does it for you.
    – coldspeed
    Dec 5 at 13:11






  • 1




    I feel so stupid after watching this answer :}
    – Mohit Motwani
    Dec 5 at 13:28






  • 1




    That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
    – Gorjan Radevski
    Dec 5 at 13:46















up vote
12
down vote



accepted










Why not just multiply?



df.pop('value').values * df

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3


DataFrame.pop has the nice effect of in-place removing and returning a column, so you can do this in a single step.





if the "item_*" columns have anything besides 1 in them, then you can multiply with bools:



df.pop('value').values * df.astype(bool)

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3




If your DataFrame has other columns, then do this:



df
value name item1 item2 item3
0 4 John 0 1 0
1 5 Mike 1 0 0
2 3 Stan 0 0 1

# cols = df.columns[df.columns.str.startswith('item')]
cols = df.filter(like='item').columns
df[cols] = df.pop('value').values * df[cols]

df
name item1 item2 item3
0 John 0 5 0
1 Mike 4 0 0
2 Stan 0 0 3





share|improve this answer



















  • 4




    Most elegant answer so far
    – horro
    Dec 5 at 12:53










  • I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
    – Gorjan Radevski
    Dec 5 at 13:08












  • @GorjanRadevski Let me know if the edit does it for you.
    – coldspeed
    Dec 5 at 13:11






  • 1




    I feel so stupid after watching this answer :}
    – Mohit Motwani
    Dec 5 at 13:28






  • 1




    That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
    – Gorjan Radevski
    Dec 5 at 13:46













up vote
12
down vote



accepted







up vote
12
down vote



accepted






Why not just multiply?



df.pop('value').values * df

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3


DataFrame.pop has the nice effect of in-place removing and returning a column, so you can do this in a single step.





if the "item_*" columns have anything besides 1 in them, then you can multiply with bools:



df.pop('value').values * df.astype(bool)

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3




If your DataFrame has other columns, then do this:



df
value name item1 item2 item3
0 4 John 0 1 0
1 5 Mike 1 0 0
2 3 Stan 0 0 1

# cols = df.columns[df.columns.str.startswith('item')]
cols = df.filter(like='item').columns
df[cols] = df.pop('value').values * df[cols]

df
name item1 item2 item3
0 John 0 5 0
1 Mike 4 0 0
2 Stan 0 0 3





share|improve this answer














Why not just multiply?



df.pop('value').values * df

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3


DataFrame.pop has the nice effect of in-place removing and returning a column, so you can do this in a single step.





if the "item_*" columns have anything besides 1 in them, then you can multiply with bools:



df.pop('value').values * df.astype(bool)

item1 item2 item3
0 0 5 0
1 4 0 0
2 0 0 3




If your DataFrame has other columns, then do this:



df
value name item1 item2 item3
0 4 John 0 1 0
1 5 Mike 1 0 0
2 3 Stan 0 0 1

# cols = df.columns[df.columns.str.startswith('item')]
cols = df.filter(like='item').columns
df[cols] = df.pop('value').values * df[cols]

df
name item1 item2 item3
0 John 0 5 0
1 Mike 4 0 0
2 Stan 0 0 3






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 13:09

























answered Dec 5 at 12:51









coldspeed

113k18104176




113k18104176








  • 4




    Most elegant answer so far
    – horro
    Dec 5 at 12:53










  • I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
    – Gorjan Radevski
    Dec 5 at 13:08












  • @GorjanRadevski Let me know if the edit does it for you.
    – coldspeed
    Dec 5 at 13:11






  • 1




    I feel so stupid after watching this answer :}
    – Mohit Motwani
    Dec 5 at 13:28






  • 1




    That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
    – Gorjan Radevski
    Dec 5 at 13:46














  • 4




    Most elegant answer so far
    – horro
    Dec 5 at 12:53










  • I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
    – Gorjan Radevski
    Dec 5 at 13:08












  • @GorjanRadevski Let me know if the edit does it for you.
    – coldspeed
    Dec 5 at 13:11






  • 1




    I feel so stupid after watching this answer :}
    – Mohit Motwani
    Dec 5 at 13:28






  • 1




    That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
    – Gorjan Radevski
    Dec 5 at 13:46








4




4




Most elegant answer so far
– horro
Dec 5 at 12:53




Most elegant answer so far
– horro
Dec 5 at 12:53












I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
– Gorjan Radevski
Dec 5 at 13:08






I like it but I should have been more specific with my question. Here is how my data frame actually looks like: df_in = pd.DataFrame({"value": [4, 5, 3], "name": ["John", "Mike", "Stan"], "item1": [0, 1, 0], "item2": [1, 0, 0], "item3": [0, 0, 1]}) And the output df should be: df_out = pd.DataFrame({"name": ["John", "Mike", "Stan"], "item1": [0, 4, 0], "item2": [5, 0, 0], "item3": [0, 0, 3]})
– Gorjan Radevski
Dec 5 at 13:08














@GorjanRadevski Let me know if the edit does it for you.
– coldspeed
Dec 5 at 13:11




@GorjanRadevski Let me know if the edit does it for you.
– coldspeed
Dec 5 at 13:11




1




1




I feel so stupid after watching this answer :}
– Mohit Motwani
Dec 5 at 13:28




I feel so stupid after watching this answer :}
– Mohit Motwani
Dec 5 at 13:28




1




1




That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
– Gorjan Radevski
Dec 5 at 13:46




That works! I have no idea why on the sample data frame it worked without the addition. Thank you!
– Gorjan Radevski
Dec 5 at 13:46












up vote
1
down vote













You could do something like:



df = pd.DataFrame([df['value']*df['item1'],df['value']*df['item2'],df['value']*df['item3']])
df.columns = ['item1','item2','item3']


EDIT:
As this answer will not scale well to many columns as @coldspeed comments, it should be done iterating a loop:



 cols = ['item1','item2','item3']
for c in cols:
df[c] *= df['value']
df.drop('value',axis=1,inplace=True)





share|improve this answer



















  • 1




    This won't scale well to many columns.
    – coldspeed
    Dec 5 at 12:52






  • 1




    Fair point, it should be done iterating a loop
    – horro
    Dec 5 at 12:53















up vote
1
down vote













You could do something like:



df = pd.DataFrame([df['value']*df['item1'],df['value']*df['item2'],df['value']*df['item3']])
df.columns = ['item1','item2','item3']


EDIT:
As this answer will not scale well to many columns as @coldspeed comments, it should be done iterating a loop:



 cols = ['item1','item2','item3']
for c in cols:
df[c] *= df['value']
df.drop('value',axis=1,inplace=True)





share|improve this answer



















  • 1




    This won't scale well to many columns.
    – coldspeed
    Dec 5 at 12:52






  • 1




    Fair point, it should be done iterating a loop
    – horro
    Dec 5 at 12:53













up vote
1
down vote










up vote
1
down vote









You could do something like:



df = pd.DataFrame([df['value']*df['item1'],df['value']*df['item2'],df['value']*df['item3']])
df.columns = ['item1','item2','item3']


EDIT:
As this answer will not scale well to many columns as @coldspeed comments, it should be done iterating a loop:



 cols = ['item1','item2','item3']
for c in cols:
df[c] *= df['value']
df.drop('value',axis=1,inplace=True)





share|improve this answer














You could do something like:



df = pd.DataFrame([df['value']*df['item1'],df['value']*df['item2'],df['value']*df['item3']])
df.columns = ['item1','item2','item3']


EDIT:
As this answer will not scale well to many columns as @coldspeed comments, it should be done iterating a loop:



 cols = ['item1','item2','item3']
for c in cols:
df[c] *= df['value']
df.drop('value',axis=1,inplace=True)






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 12:57

























answered Dec 5 at 12:50









horro

4681727




4681727








  • 1




    This won't scale well to many columns.
    – coldspeed
    Dec 5 at 12:52






  • 1




    Fair point, it should be done iterating a loop
    – horro
    Dec 5 at 12:53














  • 1




    This won't scale well to many columns.
    – coldspeed
    Dec 5 at 12:52






  • 1




    Fair point, it should be done iterating a loop
    – horro
    Dec 5 at 12:53








1




1




This won't scale well to many columns.
– coldspeed
Dec 5 at 12:52




This won't scale well to many columns.
– coldspeed
Dec 5 at 12:52




1




1




Fair point, it should be done iterating a loop
– horro
Dec 5 at 12:53




Fair point, it should be done iterating a loop
– horro
Dec 5 at 12:53










up vote
0
down vote













You need:



col = ['item1','item2','item3']

for c in col:
df[c] = df[c] * df['value']

df.drop(['value'],1,inplace=True)





share|improve this answer



















  • 1




    Surely you can think of something better than iteration...
    – coldspeed
    Dec 5 at 12:52















up vote
0
down vote













You need:



col = ['item1','item2','item3']

for c in col:
df[c] = df[c] * df['value']

df.drop(['value'],1,inplace=True)





share|improve this answer



















  • 1




    Surely you can think of something better than iteration...
    – coldspeed
    Dec 5 at 12:52













up vote
0
down vote










up vote
0
down vote









You need:



col = ['item1','item2','item3']

for c in col:
df[c] = df[c] * df['value']

df.drop(['value'],1,inplace=True)





share|improve this answer














You need:



col = ['item1','item2','item3']

for c in col:
df[c] = df[c] * df['value']

df.drop(['value'],1,inplace=True)






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 12:52

























answered Dec 5 at 12:51









Sociopath

3,30971535




3,30971535








  • 1




    Surely you can think of something better than iteration...
    – coldspeed
    Dec 5 at 12:52














  • 1




    Surely you can think of something better than iteration...
    – coldspeed
    Dec 5 at 12:52








1




1




Surely you can think of something better than iteration...
– coldspeed
Dec 5 at 12:52




Surely you can think of something better than iteration...
– coldspeed
Dec 5 at 12:52










up vote
0
down vote













You can use np.where:



items = ['item1', 'item2', 'item3']
for item in items:
df[item] = np.where(df[item]==1, df['value'], 0)
df.drop(columns = ['value'], inplace =True)
df
item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3





share|improve this answer



















  • 1




    Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
    – coldspeed
    Dec 5 at 12:53










  • @coldspeed You're right
    – Mohit Motwani
    Dec 5 at 12:56















up vote
0
down vote













You can use np.where:



items = ['item1', 'item2', 'item3']
for item in items:
df[item] = np.where(df[item]==1, df['value'], 0)
df.drop(columns = ['value'], inplace =True)
df
item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3





share|improve this answer



















  • 1




    Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
    – coldspeed
    Dec 5 at 12:53










  • @coldspeed You're right
    – Mohit Motwani
    Dec 5 at 12:56













up vote
0
down vote










up vote
0
down vote









You can use np.where:



items = ['item1', 'item2', 'item3']
for item in items:
df[item] = np.where(df[item]==1, df['value'], 0)
df.drop(columns = ['value'], inplace =True)
df
item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3





share|improve this answer














You can use np.where:



items = ['item1', 'item2', 'item3']
for item in items:
df[item] = np.where(df[item]==1, df['value'], 0)
df.drop(columns = ['value'], inplace =True)
df
item1 item2 item3
0 0 4 0
1 5 0 0
2 0 0 3






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 5 at 12:54

























answered Dec 5 at 12:49









Mohit Motwani

825320




825320








  • 1




    Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
    – coldspeed
    Dec 5 at 12:53










  • @coldspeed You're right
    – Mohit Motwani
    Dec 5 at 12:56














  • 1




    Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
    – coldspeed
    Dec 5 at 12:53










  • @coldspeed You're right
    – Mohit Motwani
    Dec 5 at 12:56








1




1




Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
– coldspeed
Dec 5 at 12:53




Again, can you think of something better than iteration? Don't forget to drop the "value" column once done.
– coldspeed
Dec 5 at 12:53












@coldspeed You're right
– Mohit Motwani
Dec 5 at 12:56




@coldspeed You're right
– Mohit Motwani
Dec 5 at 12:56










Gorjan Radevski is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Gorjan Radevski is a new contributor. Be nice, and check out our Code of Conduct.













Gorjan Radevski is a new contributor. Be nice, and check out our Code of Conduct.












Gorjan Radevski is a new contributor. Be nice, and check out our Code of Conduct.
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53632617%2freplace-1s-in-one-hot-columns-with-values-from-another-column%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

Plaza Victoria

In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...