Python Help Pandas row and Column











up vote
0
down vote

favorite












Hi I am kind of new to python, but I have a dataframe like this:



ID  NAME        NAME1      VALUE
1 Sarah orange 5
1 Roger apple 3
2 Amy pineapple 2
2 Kia pear 8


I want it like this:



ID NAME   NAME1     VALUE  NAME   NAME1  VALUE
1 Sarah orange 5 Roger apple 3
2 Amy pineapple 2 Kia pear 8


I am using pandas but not sure how I can achieve this and write to a csv. Any help would highly appreciated! Thanks!










share|improve this question




















  • 1




    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.
    – Isma
    Nov 21 at 12:27










  • You can not have same column name in a dataframe.
    – Sociopath
    Nov 21 at 12:30










  • @Sociopath Yes I meant to say that I want to rename it to Name_1 Name_2 etc...
    – dtev1345
    Nov 21 at 12:31















up vote
0
down vote

favorite












Hi I am kind of new to python, but I have a dataframe like this:



ID  NAME        NAME1      VALUE
1 Sarah orange 5
1 Roger apple 3
2 Amy pineapple 2
2 Kia pear 8


I want it like this:



ID NAME   NAME1     VALUE  NAME   NAME1  VALUE
1 Sarah orange 5 Roger apple 3
2 Amy pineapple 2 Kia pear 8


I am using pandas but not sure how I can achieve this and write to a csv. Any help would highly appreciated! Thanks!










share|improve this question




















  • 1




    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.
    – Isma
    Nov 21 at 12:27










  • You can not have same column name in a dataframe.
    – Sociopath
    Nov 21 at 12:30










  • @Sociopath Yes I meant to say that I want to rename it to Name_1 Name_2 etc...
    – dtev1345
    Nov 21 at 12:31













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Hi I am kind of new to python, but I have a dataframe like this:



ID  NAME        NAME1      VALUE
1 Sarah orange 5
1 Roger apple 3
2 Amy pineapple 2
2 Kia pear 8


I want it like this:



ID NAME   NAME1     VALUE  NAME   NAME1  VALUE
1 Sarah orange 5 Roger apple 3
2 Amy pineapple 2 Kia pear 8


I am using pandas but not sure how I can achieve this and write to a csv. Any help would highly appreciated! Thanks!










share|improve this question















Hi I am kind of new to python, but I have a dataframe like this:



ID  NAME        NAME1      VALUE
1 Sarah orange 5
1 Roger apple 3
2 Amy pineapple 2
2 Kia pear 8


I want it like this:



ID NAME   NAME1     VALUE  NAME   NAME1  VALUE
1 Sarah orange 5 Roger apple 3
2 Amy pineapple 2 Kia pear 8


I am using pandas but not sure how I can achieve this and write to a csv. Any help would highly appreciated! Thanks!







python excel pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 18:33









Malik Asad

281110




281110










asked Nov 21 at 12:26









dtev1345

6




6








  • 1




    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.
    – Isma
    Nov 21 at 12:27










  • You can not have same column name in a dataframe.
    – Sociopath
    Nov 21 at 12:30










  • @Sociopath Yes I meant to say that I want to rename it to Name_1 Name_2 etc...
    – dtev1345
    Nov 21 at 12:31














  • 1




    Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.
    – Isma
    Nov 21 at 12:27










  • You can not have same column name in a dataframe.
    – Sociopath
    Nov 21 at 12:30










  • @Sociopath Yes I meant to say that I want to rename it to Name_1 Name_2 etc...
    – dtev1345
    Nov 21 at 12:31








1




1




Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.
– Isma
Nov 21 at 12:27




Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". And more importantly, please read the Stack Overflow question checklist. You might also want to learn about Minimal, Complete, and Verifiable Examples.
– Isma
Nov 21 at 12:27












You can not have same column name in a dataframe.
– Sociopath
Nov 21 at 12:30




You can not have same column name in a dataframe.
– Sociopath
Nov 21 at 12:30












@Sociopath Yes I meant to say that I want to rename it to Name_1 Name_2 etc...
– dtev1345
Nov 21 at 12:31




@Sociopath Yes I meant to say that I want to rename it to Name_1 Name_2 etc...
– dtev1345
Nov 21 at 12:31












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Use set_index with cumcount for MultiIndex, reshape by unstack, sort MulitIndex by second level by sort_index and last flatten it by list comprehension with reset_index:



df = df.set_index(['ID',df.groupby('ID').cumcount()]).unstack().sort_index(axis=1, level=1)
#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#python bellow 3.6
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
df = df.reset_index()
print (df)
ID NAME_0 NAME1_0 VALUE_0 NAME_1 NAME1_1 VALUE_1
0 1 Sarah orange 5 Roger apple 3
1 2 Amy pineapple 2 Kia pear 8





share|improve this answer





















  • Thanks, I tried this but getting a value error, all arrays must be of same length?
    – dtev1345
    Nov 21 at 12:37










  • @dtev1345 - What code raise error? There are missing values in id ?
    – jezrael
    Nov 21 at 12:39










  • Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
    – dtev1345
    Nov 21 at 12:47










  • @dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
    – jezrael
    Nov 21 at 12:50










  • Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
    – dtev1345
    Nov 21 at 12:53











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412021%2fpython-help-pandas-row-and-column%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Use set_index with cumcount for MultiIndex, reshape by unstack, sort MulitIndex by second level by sort_index and last flatten it by list comprehension with reset_index:



df = df.set_index(['ID',df.groupby('ID').cumcount()]).unstack().sort_index(axis=1, level=1)
#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#python bellow 3.6
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
df = df.reset_index()
print (df)
ID NAME_0 NAME1_0 VALUE_0 NAME_1 NAME1_1 VALUE_1
0 1 Sarah orange 5 Roger apple 3
1 2 Amy pineapple 2 Kia pear 8





share|improve this answer





















  • Thanks, I tried this but getting a value error, all arrays must be of same length?
    – dtev1345
    Nov 21 at 12:37










  • @dtev1345 - What code raise error? There are missing values in id ?
    – jezrael
    Nov 21 at 12:39










  • Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
    – dtev1345
    Nov 21 at 12:47










  • @dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
    – jezrael
    Nov 21 at 12:50










  • Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
    – dtev1345
    Nov 21 at 12:53















up vote
0
down vote













Use set_index with cumcount for MultiIndex, reshape by unstack, sort MulitIndex by second level by sort_index and last flatten it by list comprehension with reset_index:



df = df.set_index(['ID',df.groupby('ID').cumcount()]).unstack().sort_index(axis=1, level=1)
#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#python bellow 3.6
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
df = df.reset_index()
print (df)
ID NAME_0 NAME1_0 VALUE_0 NAME_1 NAME1_1 VALUE_1
0 1 Sarah orange 5 Roger apple 3
1 2 Amy pineapple 2 Kia pear 8





share|improve this answer





















  • Thanks, I tried this but getting a value error, all arrays must be of same length?
    – dtev1345
    Nov 21 at 12:37










  • @dtev1345 - What code raise error? There are missing values in id ?
    – jezrael
    Nov 21 at 12:39










  • Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
    – dtev1345
    Nov 21 at 12:47










  • @dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
    – jezrael
    Nov 21 at 12:50










  • Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
    – dtev1345
    Nov 21 at 12:53













up vote
0
down vote










up vote
0
down vote









Use set_index with cumcount for MultiIndex, reshape by unstack, sort MulitIndex by second level by sort_index and last flatten it by list comprehension with reset_index:



df = df.set_index(['ID',df.groupby('ID').cumcount()]).unstack().sort_index(axis=1, level=1)
#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#python bellow 3.6
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
df = df.reset_index()
print (df)
ID NAME_0 NAME1_0 VALUE_0 NAME_1 NAME1_1 VALUE_1
0 1 Sarah orange 5 Roger apple 3
1 2 Amy pineapple 2 Kia pear 8





share|improve this answer












Use set_index with cumcount for MultiIndex, reshape by unstack, sort MulitIndex by second level by sort_index and last flatten it by list comprehension with reset_index:



df = df.set_index(['ID',df.groupby('ID').cumcount()]).unstack().sort_index(axis=1, level=1)
#python 3.6+
df.columns = [f'{a}_{b}' for a, b in df.columns]
#python bellow 3.6
#df.columns = ['{}_{}'.format(a,b) for a, b in df.columns]
df = df.reset_index()
print (df)
ID NAME_0 NAME1_0 VALUE_0 NAME_1 NAME1_1 VALUE_1
0 1 Sarah orange 5 Roger apple 3
1 2 Amy pineapple 2 Kia pear 8






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 12:30









jezrael

311k21247323




311k21247323












  • Thanks, I tried this but getting a value error, all arrays must be of same length?
    – dtev1345
    Nov 21 at 12:37










  • @dtev1345 - What code raise error? There are missing values in id ?
    – jezrael
    Nov 21 at 12:39










  • Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
    – dtev1345
    Nov 21 at 12:47










  • @dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
    – jezrael
    Nov 21 at 12:50










  • Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
    – dtev1345
    Nov 21 at 12:53


















  • Thanks, I tried this but getting a value error, all arrays must be of same length?
    – dtev1345
    Nov 21 at 12:37










  • @dtev1345 - What code raise error? There are missing values in id ?
    – jezrael
    Nov 21 at 12:39










  • Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
    – dtev1345
    Nov 21 at 12:47










  • @dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
    – jezrael
    Nov 21 at 12:50










  • Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
    – dtev1345
    Nov 21 at 12:53
















Thanks, I tried this but getting a value error, all arrays must be of same length?
– dtev1345
Nov 21 at 12:37




Thanks, I tried this but getting a value error, all arrays must be of same length?
– dtev1345
Nov 21 at 12:37












@dtev1345 - What code raise error? There are missing values in id ?
– jezrael
Nov 21 at 12:39




@dtev1345 - What code raise error? There are missing values in id ?
– jezrael
Nov 21 at 12:39












Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
– dtev1345
Nov 21 at 12:47




Yes, there are NaNs I think, I need to remove those rows with NaN in id and then do this I think? Also, when I write this to a csv I am doing df.to_csv('test.csv', 'index=false'). Right now, my excel looks messed up, but it should work, am I right? Thanks!
– dtev1345
Nov 21 at 12:47












@dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
– jezrael
Nov 21 at 12:50




@dtev1345 - So it depends. Simpliest is remove them by df = df.dropna(subset=['id']) before my solution. But if need them, then use df['id'] = df['id'].fillna('missing'), apply my solution and last df['id'] = df['id'].replace('missing', np.nan). Here is problem groupby remove NaNs rows by default.
– jezrael
Nov 21 at 12:50












Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
– dtev1345
Nov 21 at 12:53




Thanks! But I'm still confused on the writing this to an excel, right now the excel is looking very scattered!
– dtev1345
Nov 21 at 12:53


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412021%2fpython-help-pandas-row-and-column%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...