Adding a new column to a df each cycle of a for loopAccessing the index in 'for' loops?Add new keys to a dictionary?Iterating over dictionaries using 'for' loopsSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Magical attacks and overcoming damage resistance

Nails holding drywall

Can a level 2 Warlock take one level in rogue, then continue advancing as a warlock?

Unknown code in script

What is purpose of DB Browser(dbbrowser.aspx) under admin tool?

Can a stored procedure reference the database in which it is stored?

"The cow" OR "a cow" OR "cows" in this context

My bank got bought out, am I now going to have to start filing tax returns in a different state?

Would the change in enthalpy (ΔH) for the dissolution of urea in water be positive or negative?

Is Diceware more secure than a long passphrase?

How to be good at coming up with counter example in Topology

Why do real positive eigenvalues result in an unstable system? What about eigenvalues between 0 and 1? or 1?

Is there really no use for MD5 anymore?

Injection into a proper class and choice without regularity

Cayley's Matrix Notation

How to have a sharp product image?

What was Apollo 13's "Little Jolt" after MECO?

Where was the County of Thurn und Taxis located?

Why is the underscore command _ useful?

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

How do I reattach a shelf to the wall when it ripped out of the wall?

Is it acceptable to use working hours to read general interest books?

Is there metaphorical meaning of "aus der Haft entlassen"?

Work requires me to come in early to start computer but wont let me clock in to get paid for it



Adding a new column to a df each cycle of a for loop


Accessing the index in 'for' loops?Add new keys to a dictionary?Iterating over dictionaries using 'for' loopsSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column name“Large data” work flows using pandasSelect 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;








2















I am doing some modifications to a dataframe with a for loop. I am adding a new column every cycle of the for loop, however, I also drop this column at the end of the cycle. I would like to know if it is possible to store the values of this column per cycle, and create a new dataframe that is made of each of these columns that were generated per cycle. I am using the following code:



import numpy as np
import pandas as pd

newdf = np.zeros([1000,5])
df = pd.DataFrame(np.random.choice([0.0, 0.05], size=(1000,1000)))

for i in range(0, 10):
df['sum']= df.iloc[:, -1000:].sum(axis=1)
newdf[:,i] = df['sum']
df = df.drop('sum', 1)


However, I get the following error:




index 5 is out of bounds for axis 1 with size 5




Thanks










share|improve this question
























  • What is newdf? What shape is it? What is the index on it vs on df?

    – Nathan
    Mar 22 at 16:35











  • Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above.

    – Jonathan Budez
    Mar 22 at 16:40











  • I get no errors with your code if you make the edits I just suggested (make df a pd.DataFrame and change d.drop to df.drop).

    – Nathan
    Mar 22 at 16:48











  • Do you still have an issue/question or does this work now?

    – Nathan
    Mar 23 at 15:05











  • @Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it.

    – Jonathan Budez
    Mar 24 at 13:22

















2















I am doing some modifications to a dataframe with a for loop. I am adding a new column every cycle of the for loop, however, I also drop this column at the end of the cycle. I would like to know if it is possible to store the values of this column per cycle, and create a new dataframe that is made of each of these columns that were generated per cycle. I am using the following code:



import numpy as np
import pandas as pd

newdf = np.zeros([1000,5])
df = pd.DataFrame(np.random.choice([0.0, 0.05], size=(1000,1000)))

for i in range(0, 10):
df['sum']= df.iloc[:, -1000:].sum(axis=1)
newdf[:,i] = df['sum']
df = df.drop('sum', 1)


However, I get the following error:




index 5 is out of bounds for axis 1 with size 5




Thanks










share|improve this question
























  • What is newdf? What shape is it? What is the index on it vs on df?

    – Nathan
    Mar 22 at 16:35











  • Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above.

    – Jonathan Budez
    Mar 22 at 16:40











  • I get no errors with your code if you make the edits I just suggested (make df a pd.DataFrame and change d.drop to df.drop).

    – Nathan
    Mar 22 at 16:48











  • Do you still have an issue/question or does this work now?

    – Nathan
    Mar 23 at 15:05











  • @Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it.

    – Jonathan Budez
    Mar 24 at 13:22













2












2








2








I am doing some modifications to a dataframe with a for loop. I am adding a new column every cycle of the for loop, however, I also drop this column at the end of the cycle. I would like to know if it is possible to store the values of this column per cycle, and create a new dataframe that is made of each of these columns that were generated per cycle. I am using the following code:



import numpy as np
import pandas as pd

newdf = np.zeros([1000,5])
df = pd.DataFrame(np.random.choice([0.0, 0.05], size=(1000,1000)))

for i in range(0, 10):
df['sum']= df.iloc[:, -1000:].sum(axis=1)
newdf[:,i] = df['sum']
df = df.drop('sum', 1)


However, I get the following error:




index 5 is out of bounds for axis 1 with size 5




Thanks










share|improve this question
















I am doing some modifications to a dataframe with a for loop. I am adding a new column every cycle of the for loop, however, I also drop this column at the end of the cycle. I would like to know if it is possible to store the values of this column per cycle, and create a new dataframe that is made of each of these columns that were generated per cycle. I am using the following code:



import numpy as np
import pandas as pd

newdf = np.zeros([1000,5])
df = pd.DataFrame(np.random.choice([0.0, 0.05], size=(1000,1000)))

for i in range(0, 10):
df['sum']= df.iloc[:, -1000:].sum(axis=1)
newdf[:,i] = df['sum']
df = df.drop('sum', 1)


However, I get the following error:




index 5 is out of bounds for axis 1 with size 5




Thanks







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 13:19







Jonathan Budez

















asked Mar 22 at 16:29









Jonathan BudezJonathan Budez

586




586












  • What is newdf? What shape is it? What is the index on it vs on df?

    – Nathan
    Mar 22 at 16:35











  • Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above.

    – Jonathan Budez
    Mar 22 at 16:40











  • I get no errors with your code if you make the edits I just suggested (make df a pd.DataFrame and change d.drop to df.drop).

    – Nathan
    Mar 22 at 16:48











  • Do you still have an issue/question or does this work now?

    – Nathan
    Mar 23 at 15:05











  • @Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it.

    – Jonathan Budez
    Mar 24 at 13:22

















  • What is newdf? What shape is it? What is the index on it vs on df?

    – Nathan
    Mar 22 at 16:35











  • Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above.

    – Jonathan Budez
    Mar 22 at 16:40











  • I get no errors with your code if you make the edits I just suggested (make df a pd.DataFrame and change d.drop to df.drop).

    – Nathan
    Mar 22 at 16:48











  • Do you still have an issue/question or does this work now?

    – Nathan
    Mar 23 at 15:05











  • @Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it.

    – Jonathan Budez
    Mar 24 at 13:22
















What is newdf? What shape is it? What is the index on it vs on df?

– Nathan
Mar 22 at 16:35





What is newdf? What shape is it? What is the index on it vs on df?

– Nathan
Mar 22 at 16:35













Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above.

– Jonathan Budez
Mar 22 at 16:40





Nathan, I modified the question to clarify it, I perform other operation to the df, however, for the purpose of the question I do not think it is necessary to provide those details. The main goal is explained above.

– Jonathan Budez
Mar 22 at 16:40













I get no errors with your code if you make the edits I just suggested (make df a pd.DataFrame and change d.drop to df.drop).

– Nathan
Mar 22 at 16:48





I get no errors with your code if you make the edits I just suggested (make df a pd.DataFrame and change d.drop to df.drop).

– Nathan
Mar 22 at 16:48













Do you still have an issue/question or does this work now?

– Nathan
Mar 23 at 15:05





Do you still have an issue/question or does this work now?

– Nathan
Mar 23 at 15:05













@Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it.

– Jonathan Budez
Mar 24 at 13:22





@Nathan I have edited the question, the error rise because of the dimensions of the two dataframes. However, the dimension of the newdf on the question needs to be 1000 rows and 5 columns, that is the reason why I get that error, but I have not been able to solve it.

– Jonathan Budez
Mar 24 at 13:22












1 Answer
1






active

oldest

votes


















2














The issue occurs not because of anything that has to do with df, but because when i = 5, newdf[:, i] refers to the sixth column of a NumPy array containing only five columns. If, instead, you initialize newdf through newdf = np.zeros([1000, 10]), or loop only over range(5), then your code runs without errors.






share|improve this answer























    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%2f55303976%2fadding-a-new-column-to-a-df-each-cycle-of-a-for-loop%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









    2














    The issue occurs not because of anything that has to do with df, but because when i = 5, newdf[:, i] refers to the sixth column of a NumPy array containing only five columns. If, instead, you initialize newdf through newdf = np.zeros([1000, 10]), or loop only over range(5), then your code runs without errors.






    share|improve this answer



























      2














      The issue occurs not because of anything that has to do with df, but because when i = 5, newdf[:, i] refers to the sixth column of a NumPy array containing only five columns. If, instead, you initialize newdf through newdf = np.zeros([1000, 10]), or loop only over range(5), then your code runs without errors.






      share|improve this answer

























        2












        2








        2







        The issue occurs not because of anything that has to do with df, but because when i = 5, newdf[:, i] refers to the sixth column of a NumPy array containing only five columns. If, instead, you initialize newdf through newdf = np.zeros([1000, 10]), or loop only over range(5), then your code runs without errors.






        share|improve this answer













        The issue occurs not because of anything that has to do with df, but because when i = 5, newdf[:, i] refers to the sixth column of a NumPy array containing only five columns. If, instead, you initialize newdf through newdf = np.zeros([1000, 10]), or loop only over range(5), then your code runs without errors.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 24 at 13:30









        fugledefuglede

        8,22021642




        8,22021642





























            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%2f55303976%2fadding-a-new-column-to-a-df-each-cycle-of-a-for-loop%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현