Key error when selecting columns in pandas dataframe after read_csv











up vote
4
down vote

favorite
1












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




























    up vote
    4
    down vote

    favorite
    1












    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


























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      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

      118k11106163




      118k11106163










      asked Mar 6 '16 at 19:30









      Harry M

      1292414




      1292414
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          22
          down vote



          accepted










          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























          • You are amazing! Thanks so much!!
            – Harry M
            Mar 6 '16 at 19:45






          • 1




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


















          up vote
          0
          down vote













          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






























            up vote
            0
            down vote













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








              up vote
              22
              down vote



              accepted










              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























              • You are amazing! Thanks so much!!
                – Harry M
                Mar 6 '16 at 19:45






              • 1




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















              up vote
              22
              down vote



              accepted










              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























              • You are amazing! Thanks so much!!
                – Harry M
                Mar 6 '16 at 19:45






              • 1




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













              up vote
              22
              down vote



              accepted







              up vote
              22
              down vote



              accepted






              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









              MaxU

              118k11106163




              118k11106163












              • You are amazing! Thanks so much!!
                – Harry M
                Mar 6 '16 at 19:45






              • 1




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


















              • You are amazing! Thanks so much!!
                – Harry M
                Mar 6 '16 at 19:45






              • 1




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
















              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




              1




              1




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












              up vote
              0
              down vote













              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



























                up vote
                0
                down vote













                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

























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  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 at 5:22

























                  answered Aug 15 at 16:46









                  Bhaskar arya

                  45




                  45






















                      up vote
                      0
                      down vote













                      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

























                        up vote
                        0
                        down vote













                        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























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          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 at 22:53









                          beta

                          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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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







                              Popular posts from this blog

                              Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                              ComboBox Display Member on multiple fields

                              Is it possible to collect Nectar points via Trainline?