I want to append the rows of a dataframe as columnsWhat is the difference between Python's list methods append and extend?How to sort a dataframe by multiple column(s)Renaming columns in pandasFilter dataframe rows if value in column is in a set list of valuesAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
Reflecting Telescope Blind Spot?
Bullying by school - Submitted PhD thesis but not allowed to proceed to viva until change to new supervisor
Idiom for 'person who gets violent when drunk"
Threading data on TimeSeries
Print the phrase "And she said, 'But that's his.'" using only the alphabet
How can I detect if I'm in a subshell?
How to remove multiple elements from Set/Map AND knowing which ones were removed?
Can an open source licence be revoked if it violates employer's IP?
Why is gun control associated with the socially liberal Democratic party?
What is the difference between state-based effects and effects on the stack?
Are athletes' college degrees discounted by employers and graduate school admissions?
Why not make one big CPU core?
Manager wants to hire me; HR does not. How to proceed?
Arcane Tradition and Cost Efficiency: Learn spells on level-up, or learn them from scrolls/spellbooks?
Sakkāya-Ditthi and Self-View
What things do I only get a limited opportunity to take photos of?
Should I worry about having my credit pulled multiple times while car shopping?
How to avoid offending original culture when making conculture inspired from original
Nth term of Van Eck Sequence
What is the color associated with lukewarm?
mathrm in LaTeX
Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?
The title "Mord mit Aussicht" explained
Do items with curse of vanishing disappear from shulker boxes?
I want to append the rows of a dataframe as columns
What is the difference between Python's list methods append and extend?How to sort a dataframe by multiple column(s)Renaming columns in pandasFilter dataframe rows if value in column is in a set list of valuesAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have several small dataframes such as:
name x y z
A 1 2 3
A 1 23 4
A 3 5 6
B 0 2 3
And I want to append all the "A"s such that I get this dataframe
name x y z x2 y2 z2 x3 y3 z3
A 1 2 3 1 23 4 3 5 6
B 0 2 3 NaN-------------------> NaN
Any help would be appreciated, sorry if the above tables aren't spaced out properly
python pandas dataframe rows col
add a comment |
I have several small dataframes such as:
name x y z
A 1 2 3
A 1 23 4
A 3 5 6
B 0 2 3
And I want to append all the "A"s such that I get this dataframe
name x y z x2 y2 z2 x3 y3 z3
A 1 2 3 1 23 4 3 5 6
B 0 2 3 NaN-------------------> NaN
Any help would be appreciated, sorry if the above tables aren't spaced out properly
python pandas dataframe rows col
add a comment |
I have several small dataframes such as:
name x y z
A 1 2 3
A 1 23 4
A 3 5 6
B 0 2 3
And I want to append all the "A"s such that I get this dataframe
name x y z x2 y2 z2 x3 y3 z3
A 1 2 3 1 23 4 3 5 6
B 0 2 3 NaN-------------------> NaN
Any help would be appreciated, sorry if the above tables aren't spaced out properly
python pandas dataframe rows col
I have several small dataframes such as:
name x y z
A 1 2 3
A 1 23 4
A 3 5 6
B 0 2 3
And I want to append all the "A"s such that I get this dataframe
name x y z x2 y2 z2 x3 y3 z3
A 1 2 3 1 23 4 3 5 6
B 0 2 3 NaN-------------------> NaN
Any help would be appreciated, sorry if the above tables aren't spaced out properly
python pandas dataframe rows col
python pandas dataframe rows col
edited Mar 25 at 2:50
U9-Forward
21.7k51847
21.7k51847
asked Mar 25 at 2:46
Ashish MistryAshish Mistry
163
163
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
More like a pivot
problem after create the key by cumcount
--- I am using unstack
here
df['Newkey']=df.groupby('name').cumcount()+1
yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
yourdf
Out[20]:
x1 y1 z1 x2 y2 z2 x3 y3 z3
name
A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
add a comment |
Use pivot_table
with some couple of other functions:
df['idx'] = df.groupby('name').cumcount()+1
df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
df = df.sort_index(axis=1, level=1)
df.columns = [f'x_y' for x,y in df.columns]
df = df.reset_index()
And now:
print(df)
Reproduces:
name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
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',
autoActivateHeartbeat: false,
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%2f55330648%2fi-want-to-append-the-rows-of-a-dataframe-as-columns%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
More like a pivot
problem after create the key by cumcount
--- I am using unstack
here
df['Newkey']=df.groupby('name').cumcount()+1
yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
yourdf
Out[20]:
x1 y1 z1 x2 y2 z2 x3 y3 z3
name
A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
add a comment |
More like a pivot
problem after create the key by cumcount
--- I am using unstack
here
df['Newkey']=df.groupby('name').cumcount()+1
yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
yourdf
Out[20]:
x1 y1 z1 x2 y2 z2 x3 y3 z3
name
A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
add a comment |
More like a pivot
problem after create the key by cumcount
--- I am using unstack
here
df['Newkey']=df.groupby('name').cumcount()+1
yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
yourdf
Out[20]:
x1 y1 z1 x2 y2 z2 x3 y3 z3
name
A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
More like a pivot
problem after create the key by cumcount
--- I am using unstack
here
df['Newkey']=df.groupby('name').cumcount()+1
yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
yourdf
Out[20]:
x1 y1 z1 x2 y2 z2 x3 y3 z3
name
A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
answered Mar 25 at 2:51
WeNYoBenWeNYoBen
140k84978
140k84978
add a comment |
add a comment |
Use pivot_table
with some couple of other functions:
df['idx'] = df.groupby('name').cumcount()+1
df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
df = df.sort_index(axis=1, level=1)
df.columns = [f'x_y' for x,y in df.columns]
df = df.reset_index()
And now:
print(df)
Reproduces:
name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
add a comment |
Use pivot_table
with some couple of other functions:
df['idx'] = df.groupby('name').cumcount()+1
df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
df = df.sort_index(axis=1, level=1)
df.columns = [f'x_y' for x,y in df.columns]
df = df.reset_index()
And now:
print(df)
Reproduces:
name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
add a comment |
Use pivot_table
with some couple of other functions:
df['idx'] = df.groupby('name').cumcount()+1
df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
df = df.sort_index(axis=1, level=1)
df.columns = [f'x_y' for x,y in df.columns]
df = df.reset_index()
And now:
print(df)
Reproduces:
name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
Use pivot_table
with some couple of other functions:
df['idx'] = df.groupby('name').cumcount()+1
df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
df = df.sort_index(axis=1, level=1)
df.columns = [f'x_y' for x,y in df.columns]
df = df.reset_index()
And now:
print(df)
Reproduces:
name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN
answered Mar 25 at 2:59
U9-ForwardU9-Forward
21.7k51847
21.7k51847
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.
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%2f55330648%2fi-want-to-append-the-rows-of-a-dataframe-as-columns%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