How to convert (x, y, k) to k-matrix with x as rows, y as cols in Pandas / Python? [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
How to pivot a dataframe
1 answer
As per title, I have a Pandas dataframe containing coordinates and a value (z) like:
import pandas as pd
df = pd.DataFrame(
columns=['x', 'y', 'k'],
data=((x, y, x * y) for x in range(3) for y in range(3)))
resulting in:
x y k
0 0 0 0
1 0 1 0
2 0 2 0
3 1 0 0
4 1 1 1
5 1 2 2
6 2 0 0
7 2 1 2
8 2 2 4
I want to obtain:
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
(where x
values are now the rows and y
values are now the columns).
What would be the most Pythonic way of doing this with Pandas?
This would be similar to obtaining a dense representation of a matrix from a sparse one.
Note: the x
and y
values could be anything (not necessarily integers that happen to map nicely indexes).
p.s. I know I could do two manual loops, but that's what I am trying to avoid.
python python-3.x pandas
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 10:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
1
down vote
favorite
This question already has an answer here:
How to pivot a dataframe
1 answer
As per title, I have a Pandas dataframe containing coordinates and a value (z) like:
import pandas as pd
df = pd.DataFrame(
columns=['x', 'y', 'k'],
data=((x, y, x * y) for x in range(3) for y in range(3)))
resulting in:
x y k
0 0 0 0
1 0 1 0
2 0 2 0
3 1 0 0
4 1 1 1
5 1 2 2
6 2 0 0
7 2 1 2
8 2 2 4
I want to obtain:
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
(where x
values are now the rows and y
values are now the columns).
What would be the most Pythonic way of doing this with Pandas?
This would be similar to obtaining a dense representation of a matrix from a sparse one.
Note: the x
and y
values could be anything (not necessarily integers that happen to map nicely indexes).
p.s. I know I could do two manual loops, but that's what I am trying to avoid.
python python-3.x pandas
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 10:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That's definitely a duplicate for that, I just did not know this was calledpivot
– norok2
Nov 7 at 10:36
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
How to pivot a dataframe
1 answer
As per title, I have a Pandas dataframe containing coordinates and a value (z) like:
import pandas as pd
df = pd.DataFrame(
columns=['x', 'y', 'k'],
data=((x, y, x * y) for x in range(3) for y in range(3)))
resulting in:
x y k
0 0 0 0
1 0 1 0
2 0 2 0
3 1 0 0
4 1 1 1
5 1 2 2
6 2 0 0
7 2 1 2
8 2 2 4
I want to obtain:
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
(where x
values are now the rows and y
values are now the columns).
What would be the most Pythonic way of doing this with Pandas?
This would be similar to obtaining a dense representation of a matrix from a sparse one.
Note: the x
and y
values could be anything (not necessarily integers that happen to map nicely indexes).
p.s. I know I could do two manual loops, but that's what I am trying to avoid.
python python-3.x pandas
This question already has an answer here:
How to pivot a dataframe
1 answer
As per title, I have a Pandas dataframe containing coordinates and a value (z) like:
import pandas as pd
df = pd.DataFrame(
columns=['x', 'y', 'k'],
data=((x, y, x * y) for x in range(3) for y in range(3)))
resulting in:
x y k
0 0 0 0
1 0 1 0
2 0 2 0
3 1 0 0
4 1 1 1
5 1 2 2
6 2 0 0
7 2 1 2
8 2 2 4
I want to obtain:
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
(where x
values are now the rows and y
values are now the columns).
What would be the most Pythonic way of doing this with Pandas?
This would be similar to obtaining a dense representation of a matrix from a sparse one.
Note: the x
and y
values could be anything (not necessarily integers that happen to map nicely indexes).
p.s. I know I could do two manual loops, but that's what I am trying to avoid.
This question already has an answer here:
How to pivot a dataframe
1 answer
python python-3.x pandas
python python-3.x pandas
asked Nov 7 at 10:24
norok2
2,2761230
2,2761230
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 10:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jezrael
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 7 at 10:34
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
That's definitely a duplicate for that, I just did not know this was calledpivot
– norok2
Nov 7 at 10:36
add a comment |
That's definitely a duplicate for that, I just did not know this was calledpivot
– norok2
Nov 7 at 10:36
That's definitely a duplicate for that, I just did not know this was called
pivot
– norok2
Nov 7 at 10:36
That's definitely a duplicate for that, I just did not know this was called
pivot
– norok2
Nov 7 at 10:36
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You can use pivot:
df.pivot(index='x', columns='y', values='k')
y 0 1 2
x
0 0 0 0
1 0 1 2
2 0 2 4
And to match desired output you can use:
pd.DataFrame(df.pivot(index='x', columns='y', values='k').values)
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
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
You can use pivot:
df.pivot(index='x', columns='y', values='k')
y 0 1 2
x
0 0 0 0
1 0 1 2
2 0 2 4
And to match desired output you can use:
pd.DataFrame(df.pivot(index='x', columns='y', values='k').values)
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
add a comment |
up vote
1
down vote
accepted
You can use pivot:
df.pivot(index='x', columns='y', values='k')
y 0 1 2
x
0 0 0 0
1 0 1 2
2 0 2 4
And to match desired output you can use:
pd.DataFrame(df.pivot(index='x', columns='y', values='k').values)
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can use pivot:
df.pivot(index='x', columns='y', values='k')
y 0 1 2
x
0 0 0 0
1 0 1 2
2 0 2 4
And to match desired output you can use:
pd.DataFrame(df.pivot(index='x', columns='y', values='k').values)
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
You can use pivot:
df.pivot(index='x', columns='y', values='k')
y 0 1 2
x
0 0 0 0
1 0 1 2
2 0 2 4
And to match desired output you can use:
pd.DataFrame(df.pivot(index='x', columns='y', values='k').values)
0 1 2
0 0 0 0
1 0 1 2
2 0 2 4
answered Nov 7 at 10:30
zipa
15.5k21334
15.5k21334
add a comment |
add a comment |
That's definitely a duplicate for that, I just did not know this was called
pivot
– norok2
Nov 7 at 10:36