Chaining melt and groupby in pandas causes integer values to be cast to boolean
up vote
1
down vote
favorite
I'm trying to reshape the following dataframe:
df = pd.DataFrame({'feature': [True,False,False,True],
'id': [1,0,1,2]})
...to create a dataframe similar to the example below. Column names should now be the index, and the frequency of each unique value should be provided as a count.
Using melt and groupby almost achieves this, except that 0 and 1 (integers) are cast to False and True (boolean).
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Any suggestions for achieving the desired dataframe (without the 0 and 1 being cast to boolean) would be greatly appreciated!
python pandas pandas-groupby
add a comment |
up vote
1
down vote
favorite
I'm trying to reshape the following dataframe:
df = pd.DataFrame({'feature': [True,False,False,True],
'id': [1,0,1,2]})
...to create a dataframe similar to the example below. Column names should now be the index, and the frequency of each unique value should be provided as a count.
Using melt and groupby almost achieves this, except that 0 and 1 (integers) are cast to False and True (boolean).
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Any suggestions for achieving the desired dataframe (without the 0 and 1 being cast to boolean) would be greatly appreciated!
python pandas pandas-groupby
1
Index with type object. Since you have bool and int , it will convert to object , and in pandas 0 treat as False 1 treat as True for object conversion .
– W-B
Nov 19 at 23:42
makes sense, thanks for the explanation!
– tomp
Nov 20 at 2:29
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to reshape the following dataframe:
df = pd.DataFrame({'feature': [True,False,False,True],
'id': [1,0,1,2]})
...to create a dataframe similar to the example below. Column names should now be the index, and the frequency of each unique value should be provided as a count.
Using melt and groupby almost achieves this, except that 0 and 1 (integers) are cast to False and True (boolean).
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Any suggestions for achieving the desired dataframe (without the 0 and 1 being cast to boolean) would be greatly appreciated!
python pandas pandas-groupby
I'm trying to reshape the following dataframe:
df = pd.DataFrame({'feature': [True,False,False,True],
'id': [1,0,1,2]})
...to create a dataframe similar to the example below. Column names should now be the index, and the frequency of each unique value should be provided as a count.
Using melt and groupby almost achieves this, except that 0 and 1 (integers) are cast to False and True (boolean).
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Any suggestions for achieving the desired dataframe (without the 0 and 1 being cast to boolean) would be greatly appreciated!
python pandas pandas-groupby
python pandas pandas-groupby
asked Nov 19 at 23:26
tomp
435313
435313
1
Index with type object. Since you have bool and int , it will convert to object , and in pandas 0 treat as False 1 treat as True for object conversion .
– W-B
Nov 19 at 23:42
makes sense, thanks for the explanation!
– tomp
Nov 20 at 2:29
add a comment |
1
Index with type object. Since you have bool and int , it will convert to object , and in pandas 0 treat as False 1 treat as True for object conversion .
– W-B
Nov 19 at 23:42
makes sense, thanks for the explanation!
– tomp
Nov 20 at 2:29
1
1
Index with type object. Since you have bool and int , it will convert to object , and in pandas 0 treat as False 1 treat as True for object conversion .
– W-B
Nov 19 at 23:42
Index with type object. Since you have bool and int , it will convert to object , and in pandas 0 treat as False 1 treat as True for object conversion .
– W-B
Nov 19 at 23:42
makes sense, thanks for the explanation!
– tomp
Nov 20 at 2:29
makes sense, thanks for the explanation!
– tomp
Nov 20 at 2:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Using dtype
convert id
to str
df.id=df.id.astype(str)
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Out[81]:
freq
variable value
feature False 2
True 2
id 0 1
1 2
2 1
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
@tomp that is true
– W-B
Nov 20 at 16:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Using dtype
convert id
to str
df.id=df.id.astype(str)
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Out[81]:
freq
variable value
feature False 2
True 2
id 0 1
1 2
2 1
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
@tomp that is true
– W-B
Nov 20 at 16:25
add a comment |
up vote
1
down vote
accepted
Using dtype
convert id
to str
df.id=df.id.astype(str)
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Out[81]:
freq
variable value
feature False 2
True 2
id 0 1
1 2
2 1
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
@tomp that is true
– W-B
Nov 20 at 16:25
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Using dtype
convert id
to str
df.id=df.id.astype(str)
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Out[81]:
freq
variable value
feature False 2
True 2
id 0 1
1 2
2 1
Using dtype
convert id
to str
df.id=df.id.astype(str)
df.melt().groupby(['variable','value']).size().to_frame(name='freq')
Out[81]:
freq
variable value
feature False 2
True 2
id 0 1
1 2
2 1
answered Nov 19 at 23:43
W-B
96k72961
96k72961
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
@tomp that is true
– W-B
Nov 20 at 16:25
add a comment |
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
@tomp that is true
– W-B
Nov 20 at 16:25
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
This answers the question as it is phrased, but changing the data type to a string isn't ideal. e.g. df.id.sum() now returns '1012', rather than 4.
– tomp
Nov 20 at 15:52
@tomp that is true
– W-B
Nov 20 at 16:25
@tomp that is true
– W-B
Nov 20 at 16:25
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.
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.
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%2f53384109%2fchaining-melt-and-groupby-in-pandas-causes-integer-values-to-be-cast-to-boolean%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
Index with type object. Since you have bool and int , it will convert to object , and in pandas 0 treat as False 1 treat as True for object conversion .
– W-B
Nov 19 at 23:42
makes sense, thanks for the explanation!
– tomp
Nov 20 at 2:29