merging a excel file and a text file with similar job











up vote
0
down vote

favorite












I have an excel file that holds a data like this:



Name    Job
Damian Engineer
Rose Musician
Eric Dancer


I want to merge this with a textfile with rows with the same job:



25, Engineer
26, Dancer


So the final out put would be:



Name    Job         Age
Damian Engineer 25
Eric Dancer 26


I have written a something to start but I'm not sure how to have the final output



xls = pd.read_excel(excel_file)
excel_dict = xls.astype(str).to_dict('list')
with open('hello.txt', 'rb') as f1:
csv_reader = csv.reader(f1)
for row in csv_reader:
job = row[1]
if job in excel_dict['Job']:
excel_dict['Age'] = row[0]


Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have an excel file that holds a data like this:



    Name    Job
    Damian Engineer
    Rose Musician
    Eric Dancer


    I want to merge this with a textfile with rows with the same job:



    25, Engineer
    26, Dancer


    So the final out put would be:



    Name    Job         Age
    Damian Engineer 25
    Eric Dancer 26


    I have written a something to start but I'm not sure how to have the final output



    xls = pd.read_excel(excel_file)
    excel_dict = xls.astype(str).to_dict('list')
    with open('hello.txt', 'rb') as f1:
    csv_reader = csv.reader(f1)
    for row in csv_reader:
    job = row[1]
    if job in excel_dict['Job']:
    excel_dict['Age'] = row[0]


    Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an excel file that holds a data like this:



      Name    Job
      Damian Engineer
      Rose Musician
      Eric Dancer


      I want to merge this with a textfile with rows with the same job:



      25, Engineer
      26, Dancer


      So the final out put would be:



      Name    Job         Age
      Damian Engineer 25
      Eric Dancer 26


      I have written a something to start but I'm not sure how to have the final output



      xls = pd.read_excel(excel_file)
      excel_dict = xls.astype(str).to_dict('list')
      with open('hello.txt', 'rb') as f1:
      csv_reader = csv.reader(f1)
      for row in csv_reader:
      job = row[1]
      if job in excel_dict['Job']:
      excel_dict['Age'] = row[0]


      Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.










      share|improve this question













      I have an excel file that holds a data like this:



      Name    Job
      Damian Engineer
      Rose Musician
      Eric Dancer


      I want to merge this with a textfile with rows with the same job:



      25, Engineer
      26, Dancer


      So the final out put would be:



      Name    Job         Age
      Damian Engineer 25
      Eric Dancer 26


      I have written a something to start but I'm not sure how to have the final output



      xls = pd.read_excel(excel_file)
      excel_dict = xls.astype(str).to_dict('list')
      with open('hello.txt', 'rb') as f1:
      csv_reader = csv.reader(f1)
      for row in csv_reader:
      job = row[1]
      if job in excel_dict['Job']:
      excel_dict['Age'] = row[0]


      Could you please help me on how to achieve the final output above? I am using pandas to read the excel file and csv to read the textfile.







      python pandas csv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:52









      LearningNoob

      195




      195
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Try this:



          In [1411]: excel_df = pd.read_excel('myexcel.xlsx')

          In [1412]: excel_df
          Out[1412]:
          Name Job
          0 Damian Engineer
          1 Rose Musician
          2 Eric Dancer


          In [1415]: txt_df = pd.read_csv('hello.txt', header=None)
          In [1418]: txt_df.columns = ['Age', 'Job']

          In [1419]: txt_df
          Out[1419]:
          Age Job
          0 25 Engineer
          1 26 Dancer

          In [1447]: pd.merge(excel_df, txt_df, on='Job')
          Name Job Age
          0 Damian Engineer 25
          1 Eric Dancer 26





          share|improve this answer























          • Hi, I tried this but it is returning an empty dataframe?
            – LearningNoob
            Nov 8 at 11:16










          • Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
            – Mayank Porwal
            Nov 8 at 11:23


















          up vote
          0
          down vote













          pandas has a merge function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on and right_on



          xls = pd.read_excel(excel_file)
          other_file = pd.read_csv('hello.txt', sep=',')
          xls = xls.merge(other_file, on='Job')





          share|improve this answer




























            up vote
            0
            down vote













            This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.



            import pandas as pd

            xls = pd.read_excel('sample2.xlsx')

            csv_df = pd.read_csv('sample.txt', header=None)
            csv_df.columns = ['Age', 'Job']
            csv_df['Job'] = csv_df['Job'].map(str.strip)
            xls = xls.merge(csv_df, on='Job')

            print(xls)





            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%2f53206240%2fmerging-a-excel-file-and-a-text-file-with-similar-job%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
              0
              down vote



              accepted










              Try this:



              In [1411]: excel_df = pd.read_excel('myexcel.xlsx')

              In [1412]: excel_df
              Out[1412]:
              Name Job
              0 Damian Engineer
              1 Rose Musician
              2 Eric Dancer


              In [1415]: txt_df = pd.read_csv('hello.txt', header=None)
              In [1418]: txt_df.columns = ['Age', 'Job']

              In [1419]: txt_df
              Out[1419]:
              Age Job
              0 25 Engineer
              1 26 Dancer

              In [1447]: pd.merge(excel_df, txt_df, on='Job')
              Name Job Age
              0 Damian Engineer 25
              1 Eric Dancer 26





              share|improve this answer























              • Hi, I tried this but it is returning an empty dataframe?
                – LearningNoob
                Nov 8 at 11:16










              • Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
                – Mayank Porwal
                Nov 8 at 11:23















              up vote
              0
              down vote



              accepted










              Try this:



              In [1411]: excel_df = pd.read_excel('myexcel.xlsx')

              In [1412]: excel_df
              Out[1412]:
              Name Job
              0 Damian Engineer
              1 Rose Musician
              2 Eric Dancer


              In [1415]: txt_df = pd.read_csv('hello.txt', header=None)
              In [1418]: txt_df.columns = ['Age', 'Job']

              In [1419]: txt_df
              Out[1419]:
              Age Job
              0 25 Engineer
              1 26 Dancer

              In [1447]: pd.merge(excel_df, txt_df, on='Job')
              Name Job Age
              0 Damian Engineer 25
              1 Eric Dancer 26





              share|improve this answer























              • Hi, I tried this but it is returning an empty dataframe?
                – LearningNoob
                Nov 8 at 11:16










              • Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
                – Mayank Porwal
                Nov 8 at 11:23













              up vote
              0
              down vote



              accepted







              up vote
              0
              down vote



              accepted






              Try this:



              In [1411]: excel_df = pd.read_excel('myexcel.xlsx')

              In [1412]: excel_df
              Out[1412]:
              Name Job
              0 Damian Engineer
              1 Rose Musician
              2 Eric Dancer


              In [1415]: txt_df = pd.read_csv('hello.txt', header=None)
              In [1418]: txt_df.columns = ['Age', 'Job']

              In [1419]: txt_df
              Out[1419]:
              Age Job
              0 25 Engineer
              1 26 Dancer

              In [1447]: pd.merge(excel_df, txt_df, on='Job')
              Name Job Age
              0 Damian Engineer 25
              1 Eric Dancer 26





              share|improve this answer














              Try this:



              In [1411]: excel_df = pd.read_excel('myexcel.xlsx')

              In [1412]: excel_df
              Out[1412]:
              Name Job
              0 Damian Engineer
              1 Rose Musician
              2 Eric Dancer


              In [1415]: txt_df = pd.read_csv('hello.txt', header=None)
              In [1418]: txt_df.columns = ['Age', 'Job']

              In [1419]: txt_df
              Out[1419]:
              Age Job
              0 25 Engineer
              1 26 Dancer

              In [1447]: pd.merge(excel_df, txt_df, on='Job')
              Name Job Age
              0 Damian Engineer 25
              1 Eric Dancer 26






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 8 at 11:24

























              answered Nov 8 at 10:59









              Mayank Porwal

              2,3291619




              2,3291619












              • Hi, I tried this but it is returning an empty dataframe?
                – LearningNoob
                Nov 8 at 11:16










              • Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
                – Mayank Porwal
                Nov 8 at 11:23


















              • Hi, I tried this but it is returning an empty dataframe?
                – LearningNoob
                Nov 8 at 11:16










              • Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
                – Mayank Porwal
                Nov 8 at 11:23
















              Hi, I tried this but it is returning an empty dataframe?
              – LearningNoob
              Nov 8 at 11:16




              Hi, I tried this but it is returning an empty dataframe?
              – LearningNoob
              Nov 8 at 11:16












              Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
              – Mayank Porwal
              Nov 8 at 11:23




              Remove spaces from your hello.txt before the names and after comma: 25,Engineer 26,Dancer Then create txt_df and do merge again.
              – Mayank Porwal
              Nov 8 at 11:23












              up vote
              0
              down vote













              pandas has a merge function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on and right_on



              xls = pd.read_excel(excel_file)
              other_file = pd.read_csv('hello.txt', sep=',')
              xls = xls.merge(other_file, on='Job')





              share|improve this answer

























                up vote
                0
                down vote













                pandas has a merge function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on and right_on



                xls = pd.read_excel(excel_file)
                other_file = pd.read_csv('hello.txt', sep=',')
                xls = xls.merge(other_file, on='Job')





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  pandas has a merge function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on and right_on



                  xls = pd.read_excel(excel_file)
                  other_file = pd.read_csv('hello.txt', sep=',')
                  xls = xls.merge(other_file, on='Job')





                  share|improve this answer












                  pandas has a merge function. This assumes that your csv has headers, if it doesn't you can provide them or use left_on and right_on



                  xls = pd.read_excel(excel_file)
                  other_file = pd.read_csv('hello.txt', sep=',')
                  xls = xls.merge(other_file, on='Job')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 11:00









                  Alex

                  649520




                  649520






















                      up vote
                      0
                      down vote













                      This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.



                      import pandas as pd

                      xls = pd.read_excel('sample2.xlsx')

                      csv_df = pd.read_csv('sample.txt', header=None)
                      csv_df.columns = ['Age', 'Job']
                      csv_df['Job'] = csv_df['Job'].map(str.strip)
                      xls = xls.merge(csv_df, on='Job')

                      print(xls)





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.



                        import pandas as pd

                        xls = pd.read_excel('sample2.xlsx')

                        csv_df = pd.read_csv('sample.txt', header=None)
                        csv_df.columns = ['Age', 'Job']
                        csv_df['Job'] = csv_df['Job'].map(str.strip)
                        xls = xls.merge(csv_df, on='Job')

                        print(xls)





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.



                          import pandas as pd

                          xls = pd.read_excel('sample2.xlsx')

                          csv_df = pd.read_csv('sample.txt', header=None)
                          csv_df.columns = ['Age', 'Job']
                          csv_df['Job'] = csv_df['Job'].map(str.strip)
                          xls = xls.merge(csv_df, on='Job')

                          print(xls)





                          share|improve this answer












                          This will work for you. You have to strip the 'Job' column or remove whitespaces from the txt file between columns.



                          import pandas as pd

                          xls = pd.read_excel('sample2.xlsx')

                          csv_df = pd.read_csv('sample.txt', header=None)
                          csv_df.columns = ['Age', 'Job']
                          csv_df['Job'] = csv_df['Job'].map(str.strip)
                          xls = xls.merge(csv_df, on='Job')

                          print(xls)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 11:32









                          L. Letovanec

                          1




                          1






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206240%2fmerging-a-excel-file-and-a-text-file-with-similar-job%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

                              Schultheiß

                              Liste der Kulturdenkmale in Wilsdruff

                              Android Play Services Check