Pandas: Get Dummies
up vote
11
down vote
favorite
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
|
show 1 more comment
up vote
11
down vote
favorite
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
1
don't you meanpd.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 justjoin
then:df.join(pd.get_dummies(df['type']))
– EdChum
Mar 29 '16 at 13:13
|
show 1 more comment
up vote
11
down vote
favorite
up vote
11
down vote
favorite
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
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
python pandas dummy-variable
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 meanpd.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 justjoin
then:df.join(pd.get_dummies(df['type']))
– EdChum
Mar 29 '16 at 13:13
|
show 1 more comment
1
don't you meanpd.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 justjoin
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
33
down vote
accepted
You can try :
df = pd.get_dummies(df, columns=['type'])
1
This works! Thanks!
– Michael Perdue
Mar 29 '16 at 13:20
add a comment |
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'])
1
This works! Thanks!
– Michael Perdue
Mar 29 '16 at 13:20
add a comment |
up vote
33
down vote
accepted
You can try :
df = pd.get_dummies(df, columns=['type'])
1
This works! Thanks!
– Michael Perdue
Mar 29 '16 at 13:20
add a comment |
up vote
33
down vote
accepted
up vote
33
down vote
accepted
You can try :
df = pd.get_dummies(df, columns=['type'])
You can try :
df = pd.get_dummies(df, columns=['type'])
answered Mar 29 '16 at 13:18
Till
1,509614
1,509614
1
This works! Thanks!
– Michael Perdue
Mar 29 '16 at 13:20
add a comment |
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
add a comment |
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%2f36285155%2fpandas-get-dummies%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
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