Pandas: Get Dummies











up vote
11
down vote

favorite
2












I have the following dataframe:



   amount  catcode    cid      cycle      date     di  feccandid    type
0 1000 E1600 N00029285 2014 2014-05-15 D H8TX22107 24K
1 5000 G4600 N00026722 2014 2013-10-22 D H4TX28046 24K
2 4 C2100 N00030676 2014 2014-03-26 D H0MO07113 24Z


I want to make dummy variables for the values in column type. There about 15. I have tried this:



pd.get_dummies(df['type'])



And it returns this:



           24A  24C  24E  24F  24K  24N  24P  24R  24Z
date
2014-05-15 0 0 0 0 1 0 0 0 0
2013-10-22 0 0 0 0 1 0 0 0 0
2014-03-26 0 0 0 0 0 0 0 0 1


What I would like is to have a dummy variable column for each unique value in Type










share|improve this question




















  • 1




    don't you mean pd.get_dummies(df['type'])?
    – EdChum
    Mar 29 '16 at 13:08










  • Yes! thank you. Now is there a way to add it do my df or should I just do a join?
    – Michael Perdue
    Mar 29 '16 at 13:10






  • 1




    What do you expect the final df to actually look like though?
    – EdChum
    Mar 29 '16 at 13:11












  • The new fd should include the dummy columns in the new df
    – Michael Perdue
    Mar 29 '16 at 13:12






  • 2




    So you can just join then: df.join(pd.get_dummies(df['type']))
    – EdChum
    Mar 29 '16 at 13:13

















up vote
11
down vote

favorite
2












I have the following dataframe:



   amount  catcode    cid      cycle      date     di  feccandid    type
0 1000 E1600 N00029285 2014 2014-05-15 D H8TX22107 24K
1 5000 G4600 N00026722 2014 2013-10-22 D H4TX28046 24K
2 4 C2100 N00030676 2014 2014-03-26 D H0MO07113 24Z


I want to make dummy variables for the values in column type. There about 15. I have tried this:



pd.get_dummies(df['type'])



And it returns this:



           24A  24C  24E  24F  24K  24N  24P  24R  24Z
date
2014-05-15 0 0 0 0 1 0 0 0 0
2013-10-22 0 0 0 0 1 0 0 0 0
2014-03-26 0 0 0 0 0 0 0 0 1


What I would like is to have a dummy variable column for each unique value in Type










share|improve this question




















  • 1




    don't you mean pd.get_dummies(df['type'])?
    – EdChum
    Mar 29 '16 at 13:08










  • Yes! thank you. Now is there a way to add it do my df or should I just do a join?
    – Michael Perdue
    Mar 29 '16 at 13:10






  • 1




    What do you expect the final df to actually look like though?
    – EdChum
    Mar 29 '16 at 13:11












  • The new fd should include the dummy columns in the new df
    – Michael Perdue
    Mar 29 '16 at 13:12






  • 2




    So you can just join then: df.join(pd.get_dummies(df['type']))
    – EdChum
    Mar 29 '16 at 13:13















up vote
11
down vote

favorite
2









up vote
11
down vote

favorite
2






2





I have the following dataframe:



   amount  catcode    cid      cycle      date     di  feccandid    type
0 1000 E1600 N00029285 2014 2014-05-15 D H8TX22107 24K
1 5000 G4600 N00026722 2014 2013-10-22 D H4TX28046 24K
2 4 C2100 N00030676 2014 2014-03-26 D H0MO07113 24Z


I want to make dummy variables for the values in column type. There about 15. I have tried this:



pd.get_dummies(df['type'])



And it returns this:



           24A  24C  24E  24F  24K  24N  24P  24R  24Z
date
2014-05-15 0 0 0 0 1 0 0 0 0
2013-10-22 0 0 0 0 1 0 0 0 0
2014-03-26 0 0 0 0 0 0 0 0 1


What I would like is to have a dummy variable column for each unique value in Type










share|improve this question















I have the following dataframe:



   amount  catcode    cid      cycle      date     di  feccandid    type
0 1000 E1600 N00029285 2014 2014-05-15 D H8TX22107 24K
1 5000 G4600 N00026722 2014 2013-10-22 D H4TX28046 24K
2 4 C2100 N00030676 2014 2014-03-26 D H0MO07113 24Z


I want to make dummy variables for the values in column type. There about 15. I have tried this:



pd.get_dummies(df['type'])



And it returns this:



           24A  24C  24E  24F  24K  24N  24P  24R  24Z
date
2014-05-15 0 0 0 0 1 0 0 0 0
2013-10-22 0 0 0 0 1 0 0 0 0
2014-03-26 0 0 0 0 0 0 0 0 1


What I would like is to have a dummy variable column for each unique value in Type







python pandas dummy-variable






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 2 at 22:24









SherylHohman

4,26963245




4,26963245










asked Mar 29 '16 at 13:05









Michael Perdue

1,29151834




1,29151834








  • 1




    don't you mean pd.get_dummies(df['type'])?
    – EdChum
    Mar 29 '16 at 13:08










  • Yes! thank you. Now is there a way to add it do my df or should I just do a join?
    – Michael Perdue
    Mar 29 '16 at 13:10






  • 1




    What do you expect the final df to actually look like though?
    – EdChum
    Mar 29 '16 at 13:11












  • The new fd should include the dummy columns in the new df
    – Michael Perdue
    Mar 29 '16 at 13:12






  • 2




    So you can just join then: df.join(pd.get_dummies(df['type']))
    – EdChum
    Mar 29 '16 at 13:13
















  • 1




    don't you mean pd.get_dummies(df['type'])?
    – EdChum
    Mar 29 '16 at 13:08










  • Yes! thank you. Now is there a way to add it do my df or should I just do a join?
    – Michael Perdue
    Mar 29 '16 at 13:10






  • 1




    What do you expect the final df to actually look like though?
    – EdChum
    Mar 29 '16 at 13:11












  • The new fd should include the dummy columns in the new df
    – Michael Perdue
    Mar 29 '16 at 13:12






  • 2




    So you can just join then: df.join(pd.get_dummies(df['type']))
    – EdChum
    Mar 29 '16 at 13:13










1




1




don't you mean pd.get_dummies(df['type'])?
– EdChum
Mar 29 '16 at 13:08




don't you mean pd.get_dummies(df['type'])?
– EdChum
Mar 29 '16 at 13:08












Yes! thank you. Now is there a way to add it do my df or should I just do a join?
– Michael Perdue
Mar 29 '16 at 13:10




Yes! thank you. Now is there a way to add it do my df or should I just do a join?
– Michael Perdue
Mar 29 '16 at 13:10




1




1




What do you expect the final df to actually look like though?
– EdChum
Mar 29 '16 at 13:11






What do you expect the final df to actually look like though?
– EdChum
Mar 29 '16 at 13:11














The new fd should include the dummy columns in the new df
– Michael Perdue
Mar 29 '16 at 13:12




The new fd should include the dummy columns in the new df
– Michael Perdue
Mar 29 '16 at 13:12




2




2




So you can just join then: df.join(pd.get_dummies(df['type']))
– EdChum
Mar 29 '16 at 13:13






So you can just join then: df.join(pd.get_dummies(df['type']))
– EdChum
Mar 29 '16 at 13:13














1 Answer
1






active

oldest

votes

















up vote
33
down vote



accepted










You can try :



df = pd.get_dummies(df, columns=['type'])





share|improve this answer

















  • 1




    This works! Thanks!
    – Michael Perdue
    Mar 29 '16 at 13:20











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


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36285155%2fpandas-get-dummies%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
33
down vote



accepted










You can try :



df = pd.get_dummies(df, columns=['type'])





share|improve this answer

















  • 1




    This works! Thanks!
    – Michael Perdue
    Mar 29 '16 at 13:20















up vote
33
down vote



accepted










You can try :



df = pd.get_dummies(df, columns=['type'])





share|improve this answer

















  • 1




    This works! Thanks!
    – Michael Perdue
    Mar 29 '16 at 13:20













up vote
33
down vote



accepted







up vote
33
down vote



accepted






You can try :



df = pd.get_dummies(df, columns=['type'])





share|improve this answer












You can try :



df = pd.get_dummies(df, columns=['type'])






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 29 '16 at 13:18









Till

1,509614




1,509614








  • 1




    This works! Thanks!
    – Michael Perdue
    Mar 29 '16 at 13:20














  • 1




    This works! Thanks!
    – Michael Perdue
    Mar 29 '16 at 13:20








1




1




This works! Thanks!
– Michael Perdue
Mar 29 '16 at 13:20




This works! Thanks!
– Michael Perdue
Mar 29 '16 at 13:20


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36285155%2fpandas-get-dummies%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python