Pandas dataframe narrow to wide with pivot table no aggregation
up vote
2
down vote
favorite
I have a pandas dataframe that contains the iris
dataset. I want to subset this dataframe to only include sepal_length
and species
, and then reshape it so that the columns are the unique values for species
and the values are the values for that species.
# load data into a dataframe
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
head(df)
+----+---------------+--------------+---------------+--------------+---------+
| | sepal_length | sepal_width | petal_length | petal_width | species |
+----+---------------+--------------+---------------+--------------+---------+
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
+----+---------------+--------------+---------------+--------------+---------+
I can do this is I take the data out of Pandas as use a dictionary to reshape the data, but I can't figure out how to do it within pandas.
data = df.to_dict('records')
e =
for line in data:
e[line['species']] =
for line in data:
e[line['species']].append(line['sepal_length'])
new = pd.DataFrame(e)
This is what I want to end up with:
+----+---------+-------------+-----------+
| | setosa | versicolor | virginica |
+----+---------+-------------+-----------+
| 0 | 5.1 | 7.0 | 6.3 |
| 1 | 4.9 | 6.4 | 5.8 |
| 2 | 4.7 | 6.9 | 7.1 |
| 3 | 4.6 | 5.5 | 6.3 |
| 4 | 5.0 | 6.5 | 6.5 |
+----+---------+-------------+-----------+
I've tried using pd.crosstab(df['sepal_length'], df['species'])
but that doesn't get me what I want. I've also tried using df.pivot_table('sepal_length', columns='species')
and that also isn't it.
What am I missing here?
python pandas
add a comment |
up vote
2
down vote
favorite
I have a pandas dataframe that contains the iris
dataset. I want to subset this dataframe to only include sepal_length
and species
, and then reshape it so that the columns are the unique values for species
and the values are the values for that species.
# load data into a dataframe
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
head(df)
+----+---------------+--------------+---------------+--------------+---------+
| | sepal_length | sepal_width | petal_length | petal_width | species |
+----+---------------+--------------+---------------+--------------+---------+
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
+----+---------------+--------------+---------------+--------------+---------+
I can do this is I take the data out of Pandas as use a dictionary to reshape the data, but I can't figure out how to do it within pandas.
data = df.to_dict('records')
e =
for line in data:
e[line['species']] =
for line in data:
e[line['species']].append(line['sepal_length'])
new = pd.DataFrame(e)
This is what I want to end up with:
+----+---------+-------------+-----------+
| | setosa | versicolor | virginica |
+----+---------+-------------+-----------+
| 0 | 5.1 | 7.0 | 6.3 |
| 1 | 4.9 | 6.4 | 5.8 |
| 2 | 4.7 | 6.9 | 7.1 |
| 3 | 4.6 | 5.5 | 6.3 |
| 4 | 5.0 | 6.5 | 6.5 |
+----+---------+-------------+-----------+
I've tried using pd.crosstab(df['sepal_length'], df['species'])
but that doesn't get me what I want. I've also tried using df.pivot_table('sepal_length', columns='species')
and that also isn't it.
What am I missing here?
python pandas
2
What does the initial dataframe look like?
– today
Nov 11 at 20:14
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a pandas dataframe that contains the iris
dataset. I want to subset this dataframe to only include sepal_length
and species
, and then reshape it so that the columns are the unique values for species
and the values are the values for that species.
# load data into a dataframe
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
head(df)
+----+---------------+--------------+---------------+--------------+---------+
| | sepal_length | sepal_width | petal_length | petal_width | species |
+----+---------------+--------------+---------------+--------------+---------+
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
+----+---------------+--------------+---------------+--------------+---------+
I can do this is I take the data out of Pandas as use a dictionary to reshape the data, but I can't figure out how to do it within pandas.
data = df.to_dict('records')
e =
for line in data:
e[line['species']] =
for line in data:
e[line['species']].append(line['sepal_length'])
new = pd.DataFrame(e)
This is what I want to end up with:
+----+---------+-------------+-----------+
| | setosa | versicolor | virginica |
+----+---------+-------------+-----------+
| 0 | 5.1 | 7.0 | 6.3 |
| 1 | 4.9 | 6.4 | 5.8 |
| 2 | 4.7 | 6.9 | 7.1 |
| 3 | 4.6 | 5.5 | 6.3 |
| 4 | 5.0 | 6.5 | 6.5 |
+----+---------+-------------+-----------+
I've tried using pd.crosstab(df['sepal_length'], df['species'])
but that doesn't get me what I want. I've also tried using df.pivot_table('sepal_length', columns='species')
and that also isn't it.
What am I missing here?
python pandas
I have a pandas dataframe that contains the iris
dataset. I want to subset this dataframe to only include sepal_length
and species
, and then reshape it so that the columns are the unique values for species
and the values are the values for that species.
# load data into a dataframe
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
head(df)
+----+---------------+--------------+---------------+--------------+---------+
| | sepal_length | sepal_width | petal_length | petal_width | species |
+----+---------------+--------------+---------------+--------------+---------+
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
+----+---------------+--------------+---------------+--------------+---------+
I can do this is I take the data out of Pandas as use a dictionary to reshape the data, but I can't figure out how to do it within pandas.
data = df.to_dict('records')
e =
for line in data:
e[line['species']] =
for line in data:
e[line['species']].append(line['sepal_length'])
new = pd.DataFrame(e)
This is what I want to end up with:
+----+---------+-------------+-----------+
| | setosa | versicolor | virginica |
+----+---------+-------------+-----------+
| 0 | 5.1 | 7.0 | 6.3 |
| 1 | 4.9 | 6.4 | 5.8 |
| 2 | 4.7 | 6.9 | 7.1 |
| 3 | 4.6 | 5.5 | 6.3 |
| 4 | 5.0 | 6.5 | 6.5 |
+----+---------+-------------+-----------+
I've tried using pd.crosstab(df['sepal_length'], df['species'])
but that doesn't get me what I want. I've also tried using df.pivot_table('sepal_length', columns='species')
and that also isn't it.
What am I missing here?
python pandas
python pandas
edited Nov 11 at 21:26
asked Nov 11 at 19:28
CurtLH
6921032
6921032
2
What does the initial dataframe look like?
– today
Nov 11 at 20:14
add a comment |
2
What does the initial dataframe look like?
– today
Nov 11 at 20:14
2
2
What does the initial dataframe look like?
– today
Nov 11 at 20:14
What does the initial dataframe look like?
– today
Nov 11 at 20:14
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
IIUC you can use grouby.cumcount
on species
col and set index, then use pivot
instead of pivot_table
which does not requires an agg
func.
df1 = df.set_index(df.groupby('species').cumcount())
df1 = df1.pivot(columns='species', values='sepal_length').rename_axis(None,axis=1)
print (df1)
setosa versicolor virginica
0 5.1 7.0 6.3
1 4.9 6.4 5.8
2 4.7 6.9 7.1
3 4.6 5.5 6.3
4 5.0 6.5 6.5
add a comment |
up vote
1
down vote
What you're trying to do will take a few steps. (The code below assumes use of the standard "Iris dataset").
First, let's subset your
DataFrame
by only the columns we need.df_subset = df[['sepal_length','species']]
Next, use
pandas.pivot
(intead ofpandas.pivot_table
) to convert yourDataFrame
from "long" to "flat".df_pivot = df_subset.pivot(columns='species',values='sepal_length')
Now, we're close to what you wanted but because your three
species
columns run along the same index, the pivotedDataFrame
returnsNaN
s for two of the three columns for any given row. We can work around this by column-wise concatenating theDataFrame
while re-indexing it. (Essentially creating threeDataFrames
- one for each species - and joining them along a new index). We can do this one of two ways:The compact solution:
names = ['setosa','versicolor','virginica']
df_final = pd.concat(map(lambda name: df_pivot[name].dropna().reset_index().drop('index',axis=1), names), axis=1)Which is equivalent to:
df_final = pd.concat([
df_pivot['setosa'].dropna().reset_index().drop('index',axis=1),
df_pivot['versicolor'].dropna().reset_index().drop('index',axis=1),
df_pivot['virginica'].dropna().reset_index().drop('index',axis=1)],axis=1)
add a comment |
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
);
);
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%2f53252391%2fpandas-dataframe-narrow-to-wide-with-pivot-table-no-aggregation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
IIUC you can use grouby.cumcount
on species
col and set index, then use pivot
instead of pivot_table
which does not requires an agg
func.
df1 = df.set_index(df.groupby('species').cumcount())
df1 = df1.pivot(columns='species', values='sepal_length').rename_axis(None,axis=1)
print (df1)
setosa versicolor virginica
0 5.1 7.0 6.3
1 4.9 6.4 5.8
2 4.7 6.9 7.1
3 4.6 5.5 6.3
4 5.0 6.5 6.5
add a comment |
up vote
2
down vote
accepted
IIUC you can use grouby.cumcount
on species
col and set index, then use pivot
instead of pivot_table
which does not requires an agg
func.
df1 = df.set_index(df.groupby('species').cumcount())
df1 = df1.pivot(columns='species', values='sepal_length').rename_axis(None,axis=1)
print (df1)
setosa versicolor virginica
0 5.1 7.0 6.3
1 4.9 6.4 5.8
2 4.7 6.9 7.1
3 4.6 5.5 6.3
4 5.0 6.5 6.5
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
IIUC you can use grouby.cumcount
on species
col and set index, then use pivot
instead of pivot_table
which does not requires an agg
func.
df1 = df.set_index(df.groupby('species').cumcount())
df1 = df1.pivot(columns='species', values='sepal_length').rename_axis(None,axis=1)
print (df1)
setosa versicolor virginica
0 5.1 7.0 6.3
1 4.9 6.4 5.8
2 4.7 6.9 7.1
3 4.6 5.5 6.3
4 5.0 6.5 6.5
IIUC you can use grouby.cumcount
on species
col and set index, then use pivot
instead of pivot_table
which does not requires an agg
func.
df1 = df.set_index(df.groupby('species').cumcount())
df1 = df1.pivot(columns='species', values='sepal_length').rename_axis(None,axis=1)
print (df1)
setosa versicolor virginica
0 5.1 7.0 6.3
1 4.9 6.4 5.8
2 4.7 6.9 7.1
3 4.6 5.5 6.3
4 5.0 6.5 6.5
edited Nov 12 at 10:34
answered Nov 12 at 6:48
Abhi
2,479320
2,479320
add a comment |
add a comment |
up vote
1
down vote
What you're trying to do will take a few steps. (The code below assumes use of the standard "Iris dataset").
First, let's subset your
DataFrame
by only the columns we need.df_subset = df[['sepal_length','species']]
Next, use
pandas.pivot
(intead ofpandas.pivot_table
) to convert yourDataFrame
from "long" to "flat".df_pivot = df_subset.pivot(columns='species',values='sepal_length')
Now, we're close to what you wanted but because your three
species
columns run along the same index, the pivotedDataFrame
returnsNaN
s for two of the three columns for any given row. We can work around this by column-wise concatenating theDataFrame
while re-indexing it. (Essentially creating threeDataFrames
- one for each species - and joining them along a new index). We can do this one of two ways:The compact solution:
names = ['setosa','versicolor','virginica']
df_final = pd.concat(map(lambda name: df_pivot[name].dropna().reset_index().drop('index',axis=1), names), axis=1)Which is equivalent to:
df_final = pd.concat([
df_pivot['setosa'].dropna().reset_index().drop('index',axis=1),
df_pivot['versicolor'].dropna().reset_index().drop('index',axis=1),
df_pivot['virginica'].dropna().reset_index().drop('index',axis=1)],axis=1)
add a comment |
up vote
1
down vote
What you're trying to do will take a few steps. (The code below assumes use of the standard "Iris dataset").
First, let's subset your
DataFrame
by only the columns we need.df_subset = df[['sepal_length','species']]
Next, use
pandas.pivot
(intead ofpandas.pivot_table
) to convert yourDataFrame
from "long" to "flat".df_pivot = df_subset.pivot(columns='species',values='sepal_length')
Now, we're close to what you wanted but because your three
species
columns run along the same index, the pivotedDataFrame
returnsNaN
s for two of the three columns for any given row. We can work around this by column-wise concatenating theDataFrame
while re-indexing it. (Essentially creating threeDataFrames
- one for each species - and joining them along a new index). We can do this one of two ways:The compact solution:
names = ['setosa','versicolor','virginica']
df_final = pd.concat(map(lambda name: df_pivot[name].dropna().reset_index().drop('index',axis=1), names), axis=1)Which is equivalent to:
df_final = pd.concat([
df_pivot['setosa'].dropna().reset_index().drop('index',axis=1),
df_pivot['versicolor'].dropna().reset_index().drop('index',axis=1),
df_pivot['virginica'].dropna().reset_index().drop('index',axis=1)],axis=1)
add a comment |
up vote
1
down vote
up vote
1
down vote
What you're trying to do will take a few steps. (The code below assumes use of the standard "Iris dataset").
First, let's subset your
DataFrame
by only the columns we need.df_subset = df[['sepal_length','species']]
Next, use
pandas.pivot
(intead ofpandas.pivot_table
) to convert yourDataFrame
from "long" to "flat".df_pivot = df_subset.pivot(columns='species',values='sepal_length')
Now, we're close to what you wanted but because your three
species
columns run along the same index, the pivotedDataFrame
returnsNaN
s for two of the three columns for any given row. We can work around this by column-wise concatenating theDataFrame
while re-indexing it. (Essentially creating threeDataFrames
- one for each species - and joining them along a new index). We can do this one of two ways:The compact solution:
names = ['setosa','versicolor','virginica']
df_final = pd.concat(map(lambda name: df_pivot[name].dropna().reset_index().drop('index',axis=1), names), axis=1)Which is equivalent to:
df_final = pd.concat([
df_pivot['setosa'].dropna().reset_index().drop('index',axis=1),
df_pivot['versicolor'].dropna().reset_index().drop('index',axis=1),
df_pivot['virginica'].dropna().reset_index().drop('index',axis=1)],axis=1)
What you're trying to do will take a few steps. (The code below assumes use of the standard "Iris dataset").
First, let's subset your
DataFrame
by only the columns we need.df_subset = df[['sepal_length','species']]
Next, use
pandas.pivot
(intead ofpandas.pivot_table
) to convert yourDataFrame
from "long" to "flat".df_pivot = df_subset.pivot(columns='species',values='sepal_length')
Now, we're close to what you wanted but because your three
species
columns run along the same index, the pivotedDataFrame
returnsNaN
s for two of the three columns for any given row. We can work around this by column-wise concatenating theDataFrame
while re-indexing it. (Essentially creating threeDataFrames
- one for each species - and joining them along a new index). We can do this one of two ways:The compact solution:
names = ['setosa','versicolor','virginica']
df_final = pd.concat(map(lambda name: df_pivot[name].dropna().reset_index().drop('index',axis=1), names), axis=1)Which is equivalent to:
df_final = pd.concat([
df_pivot['setosa'].dropna().reset_index().drop('index',axis=1),
df_pivot['versicolor'].dropna().reset_index().drop('index',axis=1),
df_pivot['virginica'].dropna().reset_index().drop('index',axis=1)],axis=1)
answered Nov 11 at 21:25
dmitriys
15119
15119
add a comment |
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%2f53252391%2fpandas-dataframe-narrow-to-wide-with-pivot-table-no-aggregation%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
2
What does the initial dataframe look like?
– today
Nov 11 at 20:14