Python: Changing values in a DataFrame












0















I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:



0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5


I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.



This is what I've tried, am I going down the right path?



for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)


EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:



0  1  2  3  4  5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20









share|improve this question

























  • Just assign a value to column df[2]=3, df[3]=3, df[4]=3

    – AkshayNevrekar
    Nov 19 '18 at 11:37













  • df[[2, 3, 4]] = 3 ?

    – jpp
    Nov 19 '18 at 11:38











  • There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?

    – MEdwin
    Nov 19 '18 at 11:38


















0















I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:



0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5


I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.



This is what I've tried, am I going down the right path?



for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)


EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:



0  1  2  3  4  5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20









share|improve this question

























  • Just assign a value to column df[2]=3, df[3]=3, df[4]=3

    – AkshayNevrekar
    Nov 19 '18 at 11:37













  • df[[2, 3, 4]] = 3 ?

    – jpp
    Nov 19 '18 at 11:38











  • There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?

    – MEdwin
    Nov 19 '18 at 11:38
















0












0








0








I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:



0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5


I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.



This is what I've tried, am I going down the right path?



for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)


EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:



0  1  2  3  4  5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20









share|improve this question
















I'm new to python and pandas and I need some ideas. Say I have the following DataFrame:



0 1 2 3 4 5
1 5 5 5 5 5
2 5 5 5 5 5
3 5 5 5 5 5
4 5 5 5 5 5


I want to iterate through each row and change the values of specific columns. Say I wanted to change all of the values in columns (2,3,4) to a 3.



This is what I've tried, am I going down the right path?



for row in df.iterrows():
for col in range(2, 4):
df.set_value('row', 'col', 3)


EDIT:
Thanks for the responses. The simple solutions are obvious, but what if I wanted to change the values to this... for example:



0  1  2  3  4  5
1 1 2 3 4 5
2 6 7 8 9 10
3 11 12 13 14 15
4 16 17 18 19 20






python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 11:50







embedded.95

















asked Nov 19 '18 at 11:35









embedded.95embedded.95

227




227













  • Just assign a value to column df[2]=3, df[3]=3, df[4]=3

    – AkshayNevrekar
    Nov 19 '18 at 11:37













  • df[[2, 3, 4]] = 3 ?

    – jpp
    Nov 19 '18 at 11:38











  • There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?

    – MEdwin
    Nov 19 '18 at 11:38





















  • Just assign a value to column df[2]=3, df[3]=3, df[4]=3

    – AkshayNevrekar
    Nov 19 '18 at 11:37













  • df[[2, 3, 4]] = 3 ?

    – jpp
    Nov 19 '18 at 11:38











  • There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?

    – MEdwin
    Nov 19 '18 at 11:38



















Just assign a value to column df[2]=3, df[3]=3, df[4]=3

– AkshayNevrekar
Nov 19 '18 at 11:37







Just assign a value to column df[2]=3, df[3]=3, df[4]=3

– AkshayNevrekar
Nov 19 '18 at 11:37















df[[2, 3, 4]] = 3 ?

– jpp
Nov 19 '18 at 11:38





df[[2, 3, 4]] = 3 ?

– jpp
Nov 19 '18 at 11:38













There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?

– MEdwin
Nov 19 '18 at 11:38







There is really no reason to loop. you can call each column and asign the value. Is there anything else you will want, apart from asigning a value to the column?

– MEdwin
Nov 19 '18 at 11:38














2 Answers
2






active

oldest

votes


















2














If you are using a loop when working with dataframes, you are almost always not on the right track.



For this you can use a vectorized assignment:



df[[2, 3, 4]] = 3


Example:



df = pd.DataFrame({1: [1, 2], 2:  [1, 2]})
print(df)
# 1 2
# 0 1 1
# 1 2 2

df[[1, 2]] = 3

print(df)
# 1 2
# 0 3 3
# 1 3 3





share|improve this answer


























  • Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

    – embedded.95
    Nov 19 '18 at 11:45











  • @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

    – DeepSpace
    Nov 19 '18 at 11:49





















2














you can do this



df.iloc[:,1] = 3 #columns 2 
df.iloc[:,2] = 3
df.iloc[:,3] = 3





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%2f53373798%2fpython-changing-values-in-a-dataframe%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you are using a loop when working with dataframes, you are almost always not on the right track.



    For this you can use a vectorized assignment:



    df[[2, 3, 4]] = 3


    Example:



    df = pd.DataFrame({1: [1, 2], 2:  [1, 2]})
    print(df)
    # 1 2
    # 0 1 1
    # 1 2 2

    df[[1, 2]] = 3

    print(df)
    # 1 2
    # 0 3 3
    # 1 3 3





    share|improve this answer


























    • Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

      – embedded.95
      Nov 19 '18 at 11:45











    • @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

      – DeepSpace
      Nov 19 '18 at 11:49


















    2














    If you are using a loop when working with dataframes, you are almost always not on the right track.



    For this you can use a vectorized assignment:



    df[[2, 3, 4]] = 3


    Example:



    df = pd.DataFrame({1: [1, 2], 2:  [1, 2]})
    print(df)
    # 1 2
    # 0 1 1
    # 1 2 2

    df[[1, 2]] = 3

    print(df)
    # 1 2
    # 0 3 3
    # 1 3 3





    share|improve this answer


























    • Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

      – embedded.95
      Nov 19 '18 at 11:45











    • @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

      – DeepSpace
      Nov 19 '18 at 11:49
















    2












    2








    2







    If you are using a loop when working with dataframes, you are almost always not on the right track.



    For this you can use a vectorized assignment:



    df[[2, 3, 4]] = 3


    Example:



    df = pd.DataFrame({1: [1, 2], 2:  [1, 2]})
    print(df)
    # 1 2
    # 0 1 1
    # 1 2 2

    df[[1, 2]] = 3

    print(df)
    # 1 2
    # 0 3 3
    # 1 3 3





    share|improve this answer















    If you are using a loop when working with dataframes, you are almost always not on the right track.



    For this you can use a vectorized assignment:



    df[[2, 3, 4]] = 3


    Example:



    df = pd.DataFrame({1: [1, 2], 2:  [1, 2]})
    print(df)
    # 1 2
    # 0 1 1
    # 1 2 2

    df[[1, 2]] = 3

    print(df)
    # 1 2
    # 0 3 3
    # 1 3 3






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 19 '18 at 14:53

























    answered Nov 19 '18 at 11:38









    DeepSpaceDeepSpace

    38.6k44471




    38.6k44471













    • Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

      – embedded.95
      Nov 19 '18 at 11:45











    • @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

      – DeepSpace
      Nov 19 '18 at 11:49





















    • Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

      – embedded.95
      Nov 19 '18 at 11:45











    • @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

      – DeepSpace
      Nov 19 '18 at 11:49



















    Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

    – embedded.95
    Nov 19 '18 at 11:45





    Yes, this would be correct. But what if I wanted to apply this to 2000 rows of data, where the assigned values are different for each row. It now becomes more complicated, hence the loop. I created this dumbed down example, but I assume the principle would be the same.

    – embedded.95
    Nov 19 '18 at 11:45













    @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

    – DeepSpace
    Nov 19 '18 at 11:49







    @embedded.95 Then that is an entirely different question which has been asked and answered many times before. Use pandas indexing or np.where.

    – DeepSpace
    Nov 19 '18 at 11:49















    2














    you can do this



    df.iloc[:,1] = 3 #columns 2 
    df.iloc[:,2] = 3
    df.iloc[:,3] = 3





    share|improve this answer






























      2














      you can do this



      df.iloc[:,1] = 3 #columns 2 
      df.iloc[:,2] = 3
      df.iloc[:,3] = 3





      share|improve this answer




























        2












        2








        2







        you can do this



        df.iloc[:,1] = 3 #columns 2 
        df.iloc[:,2] = 3
        df.iloc[:,3] = 3





        share|improve this answer















        you can do this



        df.iloc[:,1] = 3 #columns 2 
        df.iloc[:,2] = 3
        df.iloc[:,3] = 3






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 11:39









        AkshayNevrekar

        4,64491736




        4,64491736










        answered Nov 19 '18 at 11:38









        runzhi xiaorunzhi xiao

        813




        813






























            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%2f53373798%2fpython-changing-values-in-a-dataframe%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()