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!
python excel pandas dataframe
add a comment |
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!
python excel pandas dataframe
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
add a comment |
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!
python excel pandas dataframe
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
python excel pandas dataframe
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
add a comment |
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
add a comment |
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
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 inid
?
– 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 bydf = df.dropna(subset=['id'])
before my solution. But if need them, then usedf['id'] = df['id'].fillna('missing')
, apply my solution and lastdf['id'] = df['id'].replace('missing', np.nan)
. Here is problemgroupby
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
|
show 5 more comments
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
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 inid
?
– 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 bydf = df.dropna(subset=['id'])
before my solution. But if need them, then usedf['id'] = df['id'].fillna('missing')
, apply my solution and lastdf['id'] = df['id'].replace('missing', np.nan)
. Here is problemgroupby
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
|
show 5 more comments
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
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 inid
?
– 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 bydf = df.dropna(subset=['id'])
before my solution. But if need them, then usedf['id'] = df['id'].fillna('missing')
, apply my solution and lastdf['id'] = df['id'].replace('missing', np.nan)
. Here is problemgroupby
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
|
show 5 more comments
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
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
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 inid
?
– 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 bydf = df.dropna(subset=['id'])
before my solution. But if need them, then usedf['id'] = df['id'].fillna('missing')
, apply my solution and lastdf['id'] = df['id'].replace('missing', np.nan)
. Here is problemgroupby
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
|
show 5 more comments
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 inid
?
– 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 bydf = df.dropna(subset=['id'])
before my solution. But if need them, then usedf['id'] = df['id'].fillna('missing')
, apply my solution and lastdf['id'] = df['id'].replace('missing', np.nan)
. Here is problemgroupby
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
|
show 5 more comments
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%2f53412021%2fpython-help-pandas-row-and-column%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
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