How do I rename columns of a df with a for loop and new column names deriving from other df?
I made a dataframe from a bigger dataframe with a for loop and and the group by function. The multiindexed dataframe has the name 'Area' for all columns and the second level for all columns is called 'mean'. I am indexing the columns by iloc and wanted to give them names based on a different dataframe.
Could you help me out? If you have a completely different approach I am thankful too, of course.
bis=len(index_df['amount'])
for c, d in zip (index_df['amount'], (bis)):
num[c] = num.iloc[:,[d]]
I get the following error message:
'TypeError: zip argument #2 must support iteration'
python pandas dataframe rename
add a comment |
I made a dataframe from a bigger dataframe with a for loop and and the group by function. The multiindexed dataframe has the name 'Area' for all columns and the second level for all columns is called 'mean'. I am indexing the columns by iloc and wanted to give them names based on a different dataframe.
Could you help me out? If you have a completely different approach I am thankful too, of course.
bis=len(index_df['amount'])
for c, d in zip (index_df['amount'], (bis)):
num[c] = num.iloc[:,[d]]
I get the following error message:
'TypeError: zip argument #2 must support iteration'
python pandas dataframe rename
What do you want to happen when youzip
over a number likebis
?
– Davis Herring
Jan 1 at 22:12
bis is 4 and the dataframe has 4 columns. I wanted to index every single column and rename it.
– Peter Klopp
Jan 1 at 22:44
add a comment |
I made a dataframe from a bigger dataframe with a for loop and and the group by function. The multiindexed dataframe has the name 'Area' for all columns and the second level for all columns is called 'mean'. I am indexing the columns by iloc and wanted to give them names based on a different dataframe.
Could you help me out? If you have a completely different approach I am thankful too, of course.
bis=len(index_df['amount'])
for c, d in zip (index_df['amount'], (bis)):
num[c] = num.iloc[:,[d]]
I get the following error message:
'TypeError: zip argument #2 must support iteration'
python pandas dataframe rename
I made a dataframe from a bigger dataframe with a for loop and and the group by function. The multiindexed dataframe has the name 'Area' for all columns and the second level for all columns is called 'mean'. I am indexing the columns by iloc and wanted to give them names based on a different dataframe.
Could you help me out? If you have a completely different approach I am thankful too, of course.
bis=len(index_df['amount'])
for c, d in zip (index_df['amount'], (bis)):
num[c] = num.iloc[:,[d]]
I get the following error message:
'TypeError: zip argument #2 must support iteration'
python pandas dataframe rename
python pandas dataframe rename
asked Jan 1 at 22:06
Peter KloppPeter Klopp
82
82
What do you want to happen when youzip
over a number likebis
?
– Davis Herring
Jan 1 at 22:12
bis is 4 and the dataframe has 4 columns. I wanted to index every single column and rename it.
– Peter Klopp
Jan 1 at 22:44
add a comment |
What do you want to happen when youzip
over a number likebis
?
– Davis Herring
Jan 1 at 22:12
bis is 4 and the dataframe has 4 columns. I wanted to index every single column and rename it.
– Peter Klopp
Jan 1 at 22:44
What do you want to happen when you
zip
over a number like bis
?– Davis Herring
Jan 1 at 22:12
What do you want to happen when you
zip
over a number like bis
?– Davis Herring
Jan 1 at 22:12
bis is 4 and the dataframe has 4 columns. I wanted to index every single column and rename it.
– Peter Klopp
Jan 1 at 22:44
bis is 4 and the dataframe has 4 columns. I wanted to index every single column and rename it.
– Peter Klopp
Jan 1 at 22:44
add a comment |
1 Answer
1
active
oldest
votes
You can't iterate over an integer, but you can iterate over a range
object, e.g. range(len(index_df.index))
. But, in any case, =
in Pandas is used for assignment, not for renaming, unless you combine it with pd.DataFrame.pop
.
If each column of num
relates to a value in index_df['amount']
, and the relation is aligned by integer position, you can simply use assignment:
num.columns = index_df['amount']
And that's it, no manual iteration involved.
1
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
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%2f53999313%2fhow-do-i-rename-columns-of-a-df-with-a-for-loop-and-new-column-names-deriving-fr%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
You can't iterate over an integer, but you can iterate over a range
object, e.g. range(len(index_df.index))
. But, in any case, =
in Pandas is used for assignment, not for renaming, unless you combine it with pd.DataFrame.pop
.
If each column of num
relates to a value in index_df['amount']
, and the relation is aligned by integer position, you can simply use assignment:
num.columns = index_df['amount']
And that's it, no manual iteration involved.
1
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
add a comment |
You can't iterate over an integer, but you can iterate over a range
object, e.g. range(len(index_df.index))
. But, in any case, =
in Pandas is used for assignment, not for renaming, unless you combine it with pd.DataFrame.pop
.
If each column of num
relates to a value in index_df['amount']
, and the relation is aligned by integer position, you can simply use assignment:
num.columns = index_df['amount']
And that's it, no manual iteration involved.
1
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
add a comment |
You can't iterate over an integer, but you can iterate over a range
object, e.g. range(len(index_df.index))
. But, in any case, =
in Pandas is used for assignment, not for renaming, unless you combine it with pd.DataFrame.pop
.
If each column of num
relates to a value in index_df['amount']
, and the relation is aligned by integer position, you can simply use assignment:
num.columns = index_df['amount']
And that's it, no manual iteration involved.
You can't iterate over an integer, but you can iterate over a range
object, e.g. range(len(index_df.index))
. But, in any case, =
in Pandas is used for assignment, not for renaming, unless you combine it with pd.DataFrame.pop
.
If each column of num
relates to a value in index_df['amount']
, and the relation is aligned by integer position, you can simply use assignment:
num.columns = index_df['amount']
And that's it, no manual iteration involved.
answered Jan 1 at 22:40


jppjpp
102k2164115
102k2164115
1
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
add a comment |
1
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
1
1
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
Thank you very much! That helped me a lot. That again underlines that I am coding on a very low level. Haha But still, how would it work with iterations? I didn't use range because (to my knowledge) I cannot iterate over two columns...
– Peter Klopp
Jan 1 at 23:59
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%2f53999313%2fhow-do-i-rename-columns-of-a-df-with-a-for-loop-and-new-column-names-deriving-fr%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
What do you want to happen when you
zip
over a number likebis
?– Davis Herring
Jan 1 at 22:12
bis is 4 and the dataframe has 4 columns. I wanted to index every single column and rename it.
– Peter Klopp
Jan 1 at 22:44