Set all columns as index, or convert a dataframe to a multiindex series
up vote
1
down vote
favorite
I have the following dataframe:
from to x ratea
1 2 0.4 10
1 4 0.6 80
1 5 0.2 10
2 3 0.2 10
2 4 0.4 10
2 6 0.3 10
3 5 0.2 10
4 6 0.3 10
My goal is to have something like the following:
from to
1 2 x 0.4
1 2 ratea 10
1 4 x 0.6
1 4 ratea 80
1 5 x 0.2
1 5 ratea 10
2 3 x 0.2
2 3 ratea 10
2 4 x 0.4
2 4 ratea 10
2 6 x 0.3
2 6 ratea 10
3 5 x 0.2
3 5 ratea 10
4 6 x 0.3
4 6 ratea 10
How would I do this conversion?
PS unstack()
did not help either (or maybe I did not use it probably).
python python-3.x pandas dataframe series
add a comment |
up vote
1
down vote
favorite
I have the following dataframe:
from to x ratea
1 2 0.4 10
1 4 0.6 80
1 5 0.2 10
2 3 0.2 10
2 4 0.4 10
2 6 0.3 10
3 5 0.2 10
4 6 0.3 10
My goal is to have something like the following:
from to
1 2 x 0.4
1 2 ratea 10
1 4 x 0.6
1 4 ratea 80
1 5 x 0.2
1 5 ratea 10
2 3 x 0.2
2 3 ratea 10
2 4 x 0.4
2 4 ratea 10
2 6 x 0.3
2 6 ratea 10
3 5 x 0.2
3 5 ratea 10
4 6 x 0.3
4 6 ratea 10
How would I do this conversion?
PS unstack()
did not help either (or maybe I did not use it probably).
python python-3.x pandas dataframe series
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following dataframe:
from to x ratea
1 2 0.4 10
1 4 0.6 80
1 5 0.2 10
2 3 0.2 10
2 4 0.4 10
2 6 0.3 10
3 5 0.2 10
4 6 0.3 10
My goal is to have something like the following:
from to
1 2 x 0.4
1 2 ratea 10
1 4 x 0.6
1 4 ratea 80
1 5 x 0.2
1 5 ratea 10
2 3 x 0.2
2 3 ratea 10
2 4 x 0.4
2 4 ratea 10
2 6 x 0.3
2 6 ratea 10
3 5 x 0.2
3 5 ratea 10
4 6 x 0.3
4 6 ratea 10
How would I do this conversion?
PS unstack()
did not help either (or maybe I did not use it probably).
python python-3.x pandas dataframe series
I have the following dataframe:
from to x ratea
1 2 0.4 10
1 4 0.6 80
1 5 0.2 10
2 3 0.2 10
2 4 0.4 10
2 6 0.3 10
3 5 0.2 10
4 6 0.3 10
My goal is to have something like the following:
from to
1 2 x 0.4
1 2 ratea 10
1 4 x 0.6
1 4 ratea 80
1 5 x 0.2
1 5 ratea 10
2 3 x 0.2
2 3 ratea 10
2 4 x 0.4
2 4 ratea 10
2 6 x 0.3
2 6 ratea 10
3 5 x 0.2
3 5 ratea 10
4 6 x 0.3
4 6 ratea 10
How would I do this conversion?
PS unstack()
did not help either (or maybe I did not use it probably).
python python-3.x pandas dataframe series
python python-3.x pandas dataframe series
asked Nov 11 at 7:58
Ali
826
826
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Use set_index
with stack
with rename_axis
for set new index names:
s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
print (s)
from to val
1 2 x 0.4
ratea 10.0
4 x 0.6
ratea 80.0
5 x 0.2
ratea 10.0
2 3 x 0.2
ratea 10.0
4 x 0.4
ratea 10.0
6 x 0.3
ratea 10.0
3 5 x 0.2
ratea 10.0
4 6 x 0.3
ratea 10.0
dtype: float64
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
1
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
1
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
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
Use set_index
with stack
with rename_axis
for set new index names:
s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
print (s)
from to val
1 2 x 0.4
ratea 10.0
4 x 0.6
ratea 80.0
5 x 0.2
ratea 10.0
2 3 x 0.2
ratea 10.0
4 x 0.4
ratea 10.0
6 x 0.3
ratea 10.0
3 5 x 0.2
ratea 10.0
4 6 x 0.3
ratea 10.0
dtype: float64
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
1
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
1
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
add a comment |
up vote
1
down vote
accepted
Use set_index
with stack
with rename_axis
for set new index names:
s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
print (s)
from to val
1 2 x 0.4
ratea 10.0
4 x 0.6
ratea 80.0
5 x 0.2
ratea 10.0
2 3 x 0.2
ratea 10.0
4 x 0.4
ratea 10.0
6 x 0.3
ratea 10.0
3 5 x 0.2
ratea 10.0
4 6 x 0.3
ratea 10.0
dtype: float64
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
1
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
1
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use set_index
with stack
with rename_axis
for set new index names:
s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
print (s)
from to val
1 2 x 0.4
ratea 10.0
4 x 0.6
ratea 80.0
5 x 0.2
ratea 10.0
2 3 x 0.2
ratea 10.0
4 x 0.4
ratea 10.0
6 x 0.3
ratea 10.0
3 5 x 0.2
ratea 10.0
4 6 x 0.3
ratea 10.0
dtype: float64
Use set_index
with stack
with rename_axis
for set new index names:
s = df.set_index(['from','to']).stack().rename_axis(['from','to','val'])
print (s)
from to val
1 2 x 0.4
ratea 10.0
4 x 0.6
ratea 80.0
5 x 0.2
ratea 10.0
2 3 x 0.2
ratea 10.0
4 x 0.4
ratea 10.0
6 x 0.3
ratea 10.0
3 5 x 0.2
ratea 10.0
4 6 x 0.3
ratea 10.0
dtype: float64
edited Nov 12 at 15:50
answered Nov 11 at 7:59
jezrael
311k21246322
311k21246322
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
1
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
1
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
add a comment |
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
1
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
1
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
Thank you very much. You don't know how many hours was wasted on this. You are a lifesaver :) You were so fast in answering that I can't accept the answer :) I have to wait 10 minutes apparently :)
– Ali
Nov 11 at 8:03
1
1
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
@Ali - You are welcome!
– jezrael
Nov 11 at 8:03
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
Is there anyway that I could name that column so I could put all of those as index, i.e. how do I convert this to a multiindex series?
– Ali
Nov 11 at 21:28
1
1
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
@Ali - check edited answer.
– jezrael
Nov 12 at 15:51
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%2f53246848%2fset-all-columns-as-index-or-convert-a-dataframe-to-a-multiindex-series%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