pandas iterrows changes ints into floats2019 Community Moderator ElectionHow do I parse a string to a float or int in Python?What is the most efficient way to loop through dataframes with pandas?Renaming columns in pandasDelete column from pandas DataFrame by column nameCreating an empty Pandas DataFrame, then filling it?“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasPython Pandas: Get index of rows which column matches certain valueUpdating value in iterrow for pandas

Under what conditions would I NOT add my Proficiency Bonus to a Spell Attack Roll (or Saving Throw DC)?

Remove object from array based on array of some property of that object

Forcing Mathematica's Integrate to give more general answers

“I had a flat in the centre of town, but I didn’t like living there, so …”

Why would the IRS ask for birth certificates or even audit a small tax return?

I've given my players a lot of magic items. Is it reasonable for me to give them harder encounters?

Named nets not connected in Eagle board design

How to make sure I'm assertive enough in contact with subordinates?

Learning to quickly identify valid fingering for piano?

What is "desert glass" and what does it do to the PCs?

Is divide-by-zero a security vulnerability?

How spaceships determine each other's mass in space?

Why dativ case for the verb widerspricht?

Sundering Titan and basic normal lands and snow lands

Are angels creatures (Mark 16:15) and can they repent (Rev 2:5 and Rom 8:21)

Preparing as much as possible of a cake in advance

ESPP--any reason not to go all in?

An Undercover Army

What is the purpose of a disclaimer like "this is not legal advice"?

Convert an array of objects to array of the objects' values

Drawing the Möbius band and the Klein bottle

How to write a chaotic neutral protagonist and prevent my readers from thinking they are evil?

Ultrafilters as a double dual

Is there a way to find out the age of climbing ropes?



pandas iterrows changes ints into floats



2019 Community Moderator ElectionHow do I parse a string to a float or int in Python?What is the most efficient way to loop through dataframes with pandas?Renaming columns in pandasDelete column from pandas DataFrame by column nameCreating an empty Pandas DataFrame, then filling it?“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasPython Pandas: Get index of rows which column matches certain valueUpdating value in iterrow for pandas










4















I'm trying to iterate over the rows of a DataFrame that contains some int64s and some floats. iterrows() seems to be turning my ints into floats, which breaks everything I want to do downstream:



>>> import pandas as pd
>>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
>>> [id for id in df.id]
[10000000000000001, 10000000000000002]
>>> [r['id'] for (idx,r) in df.iterrows()]
[10000000000000000.0, 10000000000000002.0]


Iterating directly over df.id is fine. But through iterrows(), I get different values. Is there a way to iterate over the rows in such a way that I can still index by column name and get all the correct values?










share|improve this question


























    4















    I'm trying to iterate over the rows of a DataFrame that contains some int64s and some floats. iterrows() seems to be turning my ints into floats, which breaks everything I want to do downstream:



    >>> import pandas as pd
    >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
    >>> [id for id in df.id]
    [10000000000000001, 10000000000000002]
    >>> [r['id'] for (idx,r) in df.iterrows()]
    [10000000000000000.0, 10000000000000002.0]


    Iterating directly over df.id is fine. But through iterrows(), I get different values. Is there a way to iterate over the rows in such a way that I can still index by column name and get all the correct values?










    share|improve this question
























      4












      4








      4


      1






      I'm trying to iterate over the rows of a DataFrame that contains some int64s and some floats. iterrows() seems to be turning my ints into floats, which breaks everything I want to do downstream:



      >>> import pandas as pd
      >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
      >>> [id for id in df.id]
      [10000000000000001, 10000000000000002]
      >>> [r['id'] for (idx,r) in df.iterrows()]
      [10000000000000000.0, 10000000000000002.0]


      Iterating directly over df.id is fine. But through iterrows(), I get different values. Is there a way to iterate over the rows in such a way that I can still index by column name and get all the correct values?










      share|improve this question














      I'm trying to iterate over the rows of a DataFrame that contains some int64s and some floats. iterrows() seems to be turning my ints into floats, which breaks everything I want to do downstream:



      >>> import pandas as pd
      >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
      >>> [id for id in df.id]
      [10000000000000001, 10000000000000002]
      >>> [r['id'] for (idx,r) in df.iterrows()]
      [10000000000000000.0, 10000000000000002.0]


      Iterating directly over df.id is fine. But through iterrows(), I get different values. Is there a way to iterate over the rows in such a way that I can still index by column name and get all the correct values?







      python python-2.7 pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 12 '16 at 17:18









      BarryBarry

      183k21321588




      183k21321588






















          2 Answers
          2






          active

          oldest

          votes


















          3














          Here's the relevant part of the docs:




          Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames) [...] To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster as iterrows.




          Example for your data:



          >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
          >>> [t[1] for t in df.itertuples()]
          >>> [10000000000000001, 10000000000000002]





          share|improve this answer






























            2














            If possible you're better off avoiding iteration. Check if you can vectorize your work first.



            If vectorization is impossible, you probably want DataFrame.itertuples. That will return an iterable of (named)tuples where the first element is the index label.



            In [2]: list(df.itertuples())
            Out[2]:
            [Pandas(Index=0, id=10000000000000001, prc=1.5),
            Pandas(Index=1, id=10000000000000002, prc=2.5)]


            iterrows returns a Series for each row. Since series are backed by numpy arrays, whose elements must all share a single type, your ints were cast as floats.






            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%2f34749920%2fpandas-iterrows-changes-ints-into-floats%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









              3














              Here's the relevant part of the docs:




              Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames) [...] To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster as iterrows.




              Example for your data:



              >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
              >>> [t[1] for t in df.itertuples()]
              >>> [10000000000000001, 10000000000000002]





              share|improve this answer



























                3














                Here's the relevant part of the docs:




                Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames) [...] To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster as iterrows.




                Example for your data:



                >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
                >>> [t[1] for t in df.itertuples()]
                >>> [10000000000000001, 10000000000000002]





                share|improve this answer

























                  3












                  3








                  3







                  Here's the relevant part of the docs:




                  Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames) [...] To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster as iterrows.




                  Example for your data:



                  >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
                  >>> [t[1] for t in df.itertuples()]
                  >>> [10000000000000001, 10000000000000002]





                  share|improve this answer













                  Here's the relevant part of the docs:




                  Because iterrows returns a Series for each row, it does not preserve dtypes across the rows (dtypes are preserved across columns for DataFrames) [...] To preserve dtypes while iterating over the rows, it is better to use itertuples() which returns namedtuples of the values and which is generally faster as iterrows.




                  Example for your data:



                  >>> df = pd.DataFrame([[10000000000000001, 1.5], [10000000000000002, 2.5]], columns=['id', 'prc'])
                  >>> [t[1] for t in df.itertuples()]
                  >>> [10000000000000001, 10000000000000002]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 12 '16 at 17:34









                  timgebtimgeb

                  51.1k116694




                  51.1k116694























                      2














                      If possible you're better off avoiding iteration. Check if you can vectorize your work first.



                      If vectorization is impossible, you probably want DataFrame.itertuples. That will return an iterable of (named)tuples where the first element is the index label.



                      In [2]: list(df.itertuples())
                      Out[2]:
                      [Pandas(Index=0, id=10000000000000001, prc=1.5),
                      Pandas(Index=1, id=10000000000000002, prc=2.5)]


                      iterrows returns a Series for each row. Since series are backed by numpy arrays, whose elements must all share a single type, your ints were cast as floats.






                      share|improve this answer





























                        2














                        If possible you're better off avoiding iteration. Check if you can vectorize your work first.



                        If vectorization is impossible, you probably want DataFrame.itertuples. That will return an iterable of (named)tuples where the first element is the index label.



                        In [2]: list(df.itertuples())
                        Out[2]:
                        [Pandas(Index=0, id=10000000000000001, prc=1.5),
                        Pandas(Index=1, id=10000000000000002, prc=2.5)]


                        iterrows returns a Series for each row. Since series are backed by numpy arrays, whose elements must all share a single type, your ints were cast as floats.






                        share|improve this answer



























                          2












                          2








                          2







                          If possible you're better off avoiding iteration. Check if you can vectorize your work first.



                          If vectorization is impossible, you probably want DataFrame.itertuples. That will return an iterable of (named)tuples where the first element is the index label.



                          In [2]: list(df.itertuples())
                          Out[2]:
                          [Pandas(Index=0, id=10000000000000001, prc=1.5),
                          Pandas(Index=1, id=10000000000000002, prc=2.5)]


                          iterrows returns a Series for each row. Since series are backed by numpy arrays, whose elements must all share a single type, your ints were cast as floats.






                          share|improve this answer















                          If possible you're better off avoiding iteration. Check if you can vectorize your work first.



                          If vectorization is impossible, you probably want DataFrame.itertuples. That will return an iterable of (named)tuples where the first element is the index label.



                          In [2]: list(df.itertuples())
                          Out[2]:
                          [Pandas(Index=0, id=10000000000000001, prc=1.5),
                          Pandas(Index=1, id=10000000000000002, prc=2.5)]


                          iterrows returns a Series for each row. Since series are backed by numpy arrays, whose elements must all share a single type, your ints were cast as floats.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 12 '16 at 18:23

























                          answered Jan 12 '16 at 17:28









                          TomAugspurgerTomAugspurger

                          15.8k35355




                          15.8k35355



























                              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%2f34749920%2fpandas-iterrows-changes-ints-into-floats%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

                              AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                              Алба-Юлія

                              Захаров Федір Захарович