How do I rename columns of a df with a for loop and new column names deriving from other df?












1















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'










share|improve this question























  • 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
















1















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'










share|improve this question























  • 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














1












1








1








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'










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 22:06









Peter KloppPeter Klopp

82




82













  • 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer



















  • 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













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


}
});














draft saved

draft discarded


















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









0














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.






share|improve this answer



















  • 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


















0














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.






share|improve this answer



















  • 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
















0












0








0







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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






















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.




draft saved


draft discarded














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





















































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

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory