Key error when selecting columns in pandas dataframe after read_csv










7















I'm trying to read in a CSV file into a pandas dataframe and select a column, but keep getting a key error.



The file reads in successfully and I can view the dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.



I am using this code:



import pandas as pd

transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
transactions['quarter']


This is the file I'm working on:
https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0



Thank you!










share|improve this question




























    7















    I'm trying to read in a CSV file into a pandas dataframe and select a column, but keep getting a key error.



    The file reads in successfully and I can view the dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.



    I am using this code:



    import pandas as pd

    transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
    transactions['quarter']


    This is the file I'm working on:
    https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0



    Thank you!










    share|improve this question


























      7












      7








      7


      2






      I'm trying to read in a CSV file into a pandas dataframe and select a column, but keep getting a key error.



      The file reads in successfully and I can view the dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.



      I am using this code:



      import pandas as pd

      transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
      transactions['quarter']


      This is the file I'm working on:
      https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0



      Thank you!










      share|improve this question
















      I'm trying to read in a CSV file into a pandas dataframe and select a column, but keep getting a key error.



      The file reads in successfully and I can view the dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.



      I am using this code:



      import pandas as pd

      transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
      transactions['quarter']


      This is the file I'm working on:
      https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0



      Thank you!







      python csv pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 6 '16 at 19:32









      MaxU

      123k12122175




      123k12122175










      asked Mar 6 '16 at 19:30









      Harry MHarry M

      1442415




      1442415






















          3 Answers
          3






          active

          oldest

          votes


















          29














          use sep='s*,s*' so that you will take care of spaces in column-names:



          transactions = pd.read_csv('transactions.csv', sep='s*,s*',
          header=0, encoding='ascii', engine='python')


          alternatively you can make sure that you don't have unquoted spaces in your CSV file and use your command (unchanged)



          prove:



          print(transactions.columns.tolist())


          Output:



          ['product_id', 'customer_id', 'store_id', 'promotion_id', 'month_of_year', 'quarter', 'the_year', 'store_sales', 'store_cost', 'unit_sales', 'fact_count']





          share|improve this answer




















          • 1





            You are amazing! Thanks so much!!

            – Harry M
            Mar 6 '16 at 19:45






          • 2





            the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

            – Mickey Perlstein
            Sep 2 '18 at 11:03











          • This is AMAZING! Thank you so much!

            – Sreehari R
            Jan 25 at 23:09


















          0














          I finally got the answer how to read a specific column:



          import pandas as pd 

          df=pd.read_csv('titanic.csv',sep='t')

          df['Sex']


          Because pandas separator uses t. I hope that works for you.






          share|improve this answer
































            0














            The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':



            You could also try:



            import csv
            import pandas as pd
            import re
            with open (filename, "r") as file:
            df = pd.read_csv(file, delimiter = ",")
            df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
            print(df.columns)





            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%2f35831496%2fkey-error-when-selecting-columns-in-pandas-dataframe-after-read-csv%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              29














              use sep='s*,s*' so that you will take care of spaces in column-names:



              transactions = pd.read_csv('transactions.csv', sep='s*,s*',
              header=0, encoding='ascii', engine='python')


              alternatively you can make sure that you don't have unquoted spaces in your CSV file and use your command (unchanged)



              prove:



              print(transactions.columns.tolist())


              Output:



              ['product_id', 'customer_id', 'store_id', 'promotion_id', 'month_of_year', 'quarter', 'the_year', 'store_sales', 'store_cost', 'unit_sales', 'fact_count']





              share|improve this answer




















              • 1





                You are amazing! Thanks so much!!

                – Harry M
                Mar 6 '16 at 19:45






              • 2





                the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

                – Mickey Perlstein
                Sep 2 '18 at 11:03











              • This is AMAZING! Thank you so much!

                – Sreehari R
                Jan 25 at 23:09















              29














              use sep='s*,s*' so that you will take care of spaces in column-names:



              transactions = pd.read_csv('transactions.csv', sep='s*,s*',
              header=0, encoding='ascii', engine='python')


              alternatively you can make sure that you don't have unquoted spaces in your CSV file and use your command (unchanged)



              prove:



              print(transactions.columns.tolist())


              Output:



              ['product_id', 'customer_id', 'store_id', 'promotion_id', 'month_of_year', 'quarter', 'the_year', 'store_sales', 'store_cost', 'unit_sales', 'fact_count']





              share|improve this answer




















              • 1





                You are amazing! Thanks so much!!

                – Harry M
                Mar 6 '16 at 19:45






              • 2





                the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

                – Mickey Perlstein
                Sep 2 '18 at 11:03











              • This is AMAZING! Thank you so much!

                – Sreehari R
                Jan 25 at 23:09













              29












              29








              29







              use sep='s*,s*' so that you will take care of spaces in column-names:



              transactions = pd.read_csv('transactions.csv', sep='s*,s*',
              header=0, encoding='ascii', engine='python')


              alternatively you can make sure that you don't have unquoted spaces in your CSV file and use your command (unchanged)



              prove:



              print(transactions.columns.tolist())


              Output:



              ['product_id', 'customer_id', 'store_id', 'promotion_id', 'month_of_year', 'quarter', 'the_year', 'store_sales', 'store_cost', 'unit_sales', 'fact_count']





              share|improve this answer















              use sep='s*,s*' so that you will take care of spaces in column-names:



              transactions = pd.read_csv('transactions.csv', sep='s*,s*',
              header=0, encoding='ascii', engine='python')


              alternatively you can make sure that you don't have unquoted spaces in your CSV file and use your command (unchanged)



              prove:



              print(transactions.columns.tolist())


              Output:



              ['product_id', 'customer_id', 'store_id', 'promotion_id', 'month_of_year', 'quarter', 'the_year', 'store_sales', 'store_cost', 'unit_sales', 'fact_count']






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Feb 7 '17 at 9:00

























              answered Mar 6 '16 at 19:34









              MaxUMaxU

              123k12122175




              123k12122175







              • 1





                You are amazing! Thanks so much!!

                – Harry M
                Mar 6 '16 at 19:45






              • 2





                the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

                – Mickey Perlstein
                Sep 2 '18 at 11:03











              • This is AMAZING! Thank you so much!

                – Sreehari R
                Jan 25 at 23:09












              • 1





                You are amazing! Thanks so much!!

                – Harry M
                Mar 6 '16 at 19:45






              • 2





                the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

                – Mickey Perlstein
                Sep 2 '18 at 11:03











              • This is AMAZING! Thank you so much!

                – Sreehari R
                Jan 25 at 23:09







              1




              1





              You are amazing! Thanks so much!!

              – Harry M
              Mar 6 '16 at 19:45





              You are amazing! Thanks so much!!

              – Harry M
              Mar 6 '16 at 19:45




              2




              2





              the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

              – Mickey Perlstein
              Sep 2 '18 at 11:03





              the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now

              – Mickey Perlstein
              Sep 2 '18 at 11:03













              This is AMAZING! Thank you so much!

              – Sreehari R
              Jan 25 at 23:09





              This is AMAZING! Thank you so much!

              – Sreehari R
              Jan 25 at 23:09













              0














              I finally got the answer how to read a specific column:



              import pandas as pd 

              df=pd.read_csv('titanic.csv',sep='t')

              df['Sex']


              Because pandas separator uses t. I hope that works for you.






              share|improve this answer





























                0














                I finally got the answer how to read a specific column:



                import pandas as pd 

                df=pd.read_csv('titanic.csv',sep='t')

                df['Sex']


                Because pandas separator uses t. I hope that works for you.






                share|improve this answer



























                  0












                  0








                  0







                  I finally got the answer how to read a specific column:



                  import pandas as pd 

                  df=pd.read_csv('titanic.csv',sep='t')

                  df['Sex']


                  Because pandas separator uses t. I hope that works for you.






                  share|improve this answer















                  I finally got the answer how to read a specific column:



                  import pandas as pd 

                  df=pd.read_csv('titanic.csv',sep='t')

                  df['Sex']


                  Because pandas separator uses t. I hope that works for you.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 17 '18 at 5:22

























                  answered Aug 15 '18 at 16:46









                  Bhaskar aryaBhaskar arya

                  65




                  65





















                      0














                      The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':



                      You could also try:



                      import csv
                      import pandas as pd
                      import re
                      with open (filename, "r") as file:
                      df = pd.read_csv(file, delimiter = ",")
                      df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
                      print(df.columns)





                      share|improve this answer



























                        0














                        The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':



                        You could also try:



                        import csv
                        import pandas as pd
                        import re
                        with open (filename, "r") as file:
                        df = pd.read_csv(file, delimiter = ",")
                        df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
                        print(df.columns)





                        share|improve this answer

























                          0












                          0








                          0







                          The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':



                          You could also try:



                          import csv
                          import pandas as pd
                          import re
                          with open (filename, "r") as file:
                          df = pd.read_csv(file, delimiter = ",")
                          df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
                          print(df.columns)





                          share|improve this answer













                          The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':



                          You could also try:



                          import csv
                          import pandas as pd
                          import re
                          with open (filename, "r") as file:
                          df = pd.read_csv(file, delimiter = ",")
                          df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
                          print(df.columns)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Oct 20 '18 at 22:53









                          betabeta

                          65




                          65



























                              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%2f35831496%2fkey-error-when-selecting-columns-in-pandas-dataframe-after-read-csv%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







                              這個網誌中的熱門文章

                              What does pagestruct do in Eviews?

                              Dutch intervention in Lombok and Karangasem

                              Channel Islands