Python: Changing values in a DataFrame











up vote
0
down vote

favorite












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
    – Sociopath
    Nov 19 at 11:37












  • df[[2, 3, 4]] = 3 ?
    – jpp
    Nov 19 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 at 11:38

















up vote
0
down vote

favorite












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
    – Sociopath
    Nov 19 at 11:37












  • df[[2, 3, 4]] = 3 ?
    – jpp
    Nov 19 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 at 11:38















up vote
0
down vote

favorite









up vote
0
down vote

favorite











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 at 11:50

























asked Nov 19 at 11:35









embedded.95

186




186












  • Just assign a value to column df[2]=3, df[3]=3, df[4]=3
    – Sociopath
    Nov 19 at 11:37












  • df[[2, 3, 4]] = 3 ?
    – jpp
    Nov 19 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 at 11:38




















  • Just assign a value to column df[2]=3, df[3]=3, df[4]=3
    – Sociopath
    Nov 19 at 11:37












  • df[[2, 3, 4]] = 3 ?
    – jpp
    Nov 19 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 at 11:38


















Just assign a value to column df[2]=3, df[3]=3, df[4]=3
– Sociopath
Nov 19 at 11:37






Just assign a value to column df[2]=3, df[3]=3, df[4]=3
– Sociopath
Nov 19 at 11:37














df[[2, 3, 4]] = 3 ?
– jpp
Nov 19 at 11:38




df[[2, 3, 4]] = 3 ?
– jpp
Nov 19 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 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 at 11:38














2 Answers
2






active

oldest

votes

















up vote
2
down vote













you can do this



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





share|improve this answer






























    up vote
    2
    down vote













    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 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 at 11:49













    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',
    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








    up vote
    2
    down vote













    you can do this



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





    share|improve this answer



























      up vote
      2
      down vote













      you can do this



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





      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        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 at 11:39









        Sociopath

        3,09961431




        3,09961431










        answered Nov 19 at 11:38









        runzhi xiao

        813




        813
























            up vote
            2
            down vote













            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 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 at 11:49

















            up vote
            2
            down vote













            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 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 at 11:49















            up vote
            2
            down vote










            up vote
            2
            down vote









            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 2 days ago

























            answered Nov 19 at 11:38









            DeepSpace

            34.1k43765




            34.1k43765












            • 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 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 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 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 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 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 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 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 at 11:49




















             

            draft saved


            draft discarded



















































             


            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







            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