round a single column in pandas












28















Is there a way to round a single column in pandas without affecting the rest of the dataframe?



 df:
item value1 value2
0 a 1.12 1.3
1 a 1.50 2.5
2 a 0.10 0.0
3 b 3.30 -1.0
4 b 4.80 -1.0


df.value1.apply(np.round)
gives



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


What is the correct way to make data look like this:



  item  value1  value2
0 a 1 1.3
1 a 2 2.5
2 a 0 0.0
3 b 3 -1.0
4 b 5 -1.0
5 c 5 5.0









share|improve this question



























    28















    Is there a way to round a single column in pandas without affecting the rest of the dataframe?



     df:
    item value1 value2
    0 a 1.12 1.3
    1 a 1.50 2.5
    2 a 0.10 0.0
    3 b 3.30 -1.0
    4 b 4.80 -1.0


    df.value1.apply(np.round)
    gives



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


    What is the correct way to make data look like this:



      item  value1  value2
    0 a 1 1.3
    1 a 2 2.5
    2 a 0 0.0
    3 b 3 -1.0
    4 b 5 -1.0
    5 c 5 5.0









    share|improve this question

























      28












      28








      28


      7






      Is there a way to round a single column in pandas without affecting the rest of the dataframe?



       df:
      item value1 value2
      0 a 1.12 1.3
      1 a 1.50 2.5
      2 a 0.10 0.0
      3 b 3.30 -1.0
      4 b 4.80 -1.0


      df.value1.apply(np.round)
      gives



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


      What is the correct way to make data look like this:



        item  value1  value2
      0 a 1 1.3
      1 a 2 2.5
      2 a 0 0.0
      3 b 3 -1.0
      4 b 5 -1.0
      5 c 5 5.0









      share|improve this question














      Is there a way to round a single column in pandas without affecting the rest of the dataframe?



       df:
      item value1 value2
      0 a 1.12 1.3
      1 a 1.50 2.5
      2 a 0.10 0.0
      3 b 3.30 -1.0
      4 b 4.80 -1.0


      df.value1.apply(np.round)
      gives



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


      What is the correct way to make data look like this:



        item  value1  value2
      0 a 1 1.3
      1 a 2 2.5
      2 a 0 0.0
      3 b 3 -1.0
      4 b 5 -1.0
      5 c 5 5.0






      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 1 '14 at 3:11









      k3itk3it

      4082717




      4082717
























          2 Answers
          2






          active

          oldest

          votes


















          43














          You are very close.
          You applied the round to the series of values given by df.value1.
          The return type is thus a Series.
          You need to assign that series back to the dataframe (or another dataframe with the same Index).



          Also, there is a pandas.Series.round method which is basically a short hand for pandas.Series.apply(np.round).



          In[2]: 
          df.value1 = df.value1.round()
          print df

          Out[2]:
          item value1 value2
          0 a 1 1.3
          1 a 2 2.5
          2 a 0 0.0
          3 b 3 -1.0
          4 b 5 -1.0





          share|improve this answer





















          • 1





            Thank you. not feeling very bright for missing this :)

            – k3it
            Oct 1 '14 at 4:15






          • 1





            Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

            – Lyndon White
            Oct 1 '14 at 14:57











          • @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

            – 3kstc
            Apr 11 '18 at 6:13













          • Looks like you have 3 questions there. Use the ask a question button 3 times.

            – Lyndon White
            Apr 11 '18 at 7:18



















          18














          For some reason the round() method doesn't work if you have float numbers with many decimal places, but this will.



          decimals = 2    
          df['column'] = df['column'].apply(lambda x: round(x, decimals))





          share|improve this answer





















          • 2





            df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

            – Stephan Doliov
            Feb 22 '18 at 4:50











          • @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

            – seizetheday
            Mar 2 '18 at 2:45











          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%2f26133538%2fround-a-single-column-in-pandas%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









          43














          You are very close.
          You applied the round to the series of values given by df.value1.
          The return type is thus a Series.
          You need to assign that series back to the dataframe (or another dataframe with the same Index).



          Also, there is a pandas.Series.round method which is basically a short hand for pandas.Series.apply(np.round).



          In[2]: 
          df.value1 = df.value1.round()
          print df

          Out[2]:
          item value1 value2
          0 a 1 1.3
          1 a 2 2.5
          2 a 0 0.0
          3 b 3 -1.0
          4 b 5 -1.0





          share|improve this answer





















          • 1





            Thank you. not feeling very bright for missing this :)

            – k3it
            Oct 1 '14 at 4:15






          • 1





            Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

            – Lyndon White
            Oct 1 '14 at 14:57











          • @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

            – 3kstc
            Apr 11 '18 at 6:13













          • Looks like you have 3 questions there. Use the ask a question button 3 times.

            – Lyndon White
            Apr 11 '18 at 7:18
















          43














          You are very close.
          You applied the round to the series of values given by df.value1.
          The return type is thus a Series.
          You need to assign that series back to the dataframe (or another dataframe with the same Index).



          Also, there is a pandas.Series.round method which is basically a short hand for pandas.Series.apply(np.round).



          In[2]: 
          df.value1 = df.value1.round()
          print df

          Out[2]:
          item value1 value2
          0 a 1 1.3
          1 a 2 2.5
          2 a 0 0.0
          3 b 3 -1.0
          4 b 5 -1.0





          share|improve this answer





















          • 1





            Thank you. not feeling very bright for missing this :)

            – k3it
            Oct 1 '14 at 4:15






          • 1





            Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

            – Lyndon White
            Oct 1 '14 at 14:57











          • @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

            – 3kstc
            Apr 11 '18 at 6:13













          • Looks like you have 3 questions there. Use the ask a question button 3 times.

            – Lyndon White
            Apr 11 '18 at 7:18














          43












          43








          43







          You are very close.
          You applied the round to the series of values given by df.value1.
          The return type is thus a Series.
          You need to assign that series back to the dataframe (or another dataframe with the same Index).



          Also, there is a pandas.Series.round method which is basically a short hand for pandas.Series.apply(np.round).



          In[2]: 
          df.value1 = df.value1.round()
          print df

          Out[2]:
          item value1 value2
          0 a 1 1.3
          1 a 2 2.5
          2 a 0 0.0
          3 b 3 -1.0
          4 b 5 -1.0





          share|improve this answer















          You are very close.
          You applied the round to the series of values given by df.value1.
          The return type is thus a Series.
          You need to assign that series back to the dataframe (or another dataframe with the same Index).



          Also, there is a pandas.Series.round method which is basically a short hand for pandas.Series.apply(np.round).



          In[2]: 
          df.value1 = df.value1.round()
          print df

          Out[2]:
          item value1 value2
          0 a 1 1.3
          1 a 2 2.5
          2 a 0 0.0
          3 b 3 -1.0
          4 b 5 -1.0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 8 at 10:28









          Jack

          1,8951116




          1,8951116










          answered Oct 1 '14 at 3:24









          Lyndon WhiteLyndon White

          13.7k1262101




          13.7k1262101








          • 1





            Thank you. not feeling very bright for missing this :)

            – k3it
            Oct 1 '14 at 4:15






          • 1





            Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

            – Lyndon White
            Oct 1 '14 at 14:57











          • @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

            – 3kstc
            Apr 11 '18 at 6:13













          • Looks like you have 3 questions there. Use the ask a question button 3 times.

            – Lyndon White
            Apr 11 '18 at 7:18














          • 1





            Thank you. not feeling very bright for missing this :)

            – k3it
            Oct 1 '14 at 4:15






          • 1





            Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

            – Lyndon White
            Oct 1 '14 at 14:57











          • @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

            – 3kstc
            Apr 11 '18 at 6:13













          • Looks like you have 3 questions there. Use the ask a question button 3 times.

            – Lyndon White
            Apr 11 '18 at 7:18








          1




          1





          Thank you. not feeling very bright for missing this :)

          – k3it
          Oct 1 '14 at 4:15





          Thank you. not feeling very bright for missing this :)

          – k3it
          Oct 1 '14 at 4:15




          1




          1





          Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

          – Lyndon White
          Oct 1 '14 at 14:57





          Pandas does not (to me at least) come naturally. I am fairly sure I worked out how to do this after reading another SO answer last week or so. but I couldn't find it again to mark as duplicate so it may have been a slightly different question.

          – Lyndon White
          Oct 1 '14 at 14:57













          @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

          – 3kstc
          Apr 11 '18 at 6:13







          @LyndonWhite could one use df["value1"].round() ? Also is there are a round up or round down function? And also what about if the column name has a space in it? ie value 1 - I assume df.value 1.round() would fail?

          – 3kstc
          Apr 11 '18 at 6:13















          Looks like you have 3 questions there. Use the ask a question button 3 times.

          – Lyndon White
          Apr 11 '18 at 7:18





          Looks like you have 3 questions there. Use the ask a question button 3 times.

          – Lyndon White
          Apr 11 '18 at 7:18













          18














          For some reason the round() method doesn't work if you have float numbers with many decimal places, but this will.



          decimals = 2    
          df['column'] = df['column'].apply(lambda x: round(x, decimals))





          share|improve this answer





















          • 2





            df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

            – Stephan Doliov
            Feb 22 '18 at 4:50











          • @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

            – seizetheday
            Mar 2 '18 at 2:45
















          18














          For some reason the round() method doesn't work if you have float numbers with many decimal places, but this will.



          decimals = 2    
          df['column'] = df['column'].apply(lambda x: round(x, decimals))





          share|improve this answer





















          • 2





            df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

            – Stephan Doliov
            Feb 22 '18 at 4:50











          • @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

            – seizetheday
            Mar 2 '18 at 2:45














          18












          18








          18







          For some reason the round() method doesn't work if you have float numbers with many decimal places, but this will.



          decimals = 2    
          df['column'] = df['column'].apply(lambda x: round(x, decimals))





          share|improve this answer















          For some reason the round() method doesn't work if you have float numbers with many decimal places, but this will.



          decimals = 2    
          df['column'] = df['column'].apply(lambda x: round(x, decimals))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Oct 31 '18 at 11:51

























          answered May 10 '17 at 16:15









          ReimarReimar

          599167




          599167








          • 2





            df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

            – Stephan Doliov
            Feb 22 '18 at 4:50











          • @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

            – seizetheday
            Mar 2 '18 at 2:45














          • 2





            df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

            – Stephan Doliov
            Feb 22 '18 at 4:50











          • @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

            – seizetheday
            Mar 2 '18 at 2:45








          2




          2





          df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

          – Stephan Doliov
          Feb 22 '18 at 4:50





          df.column_name.round() seems to apply to output formatting. The applying of the lambda actually changes the underlying stored value.

          – Stephan Doliov
          Feb 22 '18 at 4:50













          @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

          – seizetheday
          Mar 2 '18 at 2:45





          @StephanDoliov Exactly, precision problem bother me for a long time. round() can only set display precision not stored value.

          – seizetheday
          Mar 2 '18 at 2:45


















          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%2f26133538%2fround-a-single-column-in-pandas%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

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

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith