Using datas from a csv file to select data in another csv











up vote
0
down vote

favorite












I am very new to python and I am now stuck.



I am trying to match my field notes with the data file from my device.



I have two files record.csv and workingfile.csv.

columns in record.csv:



DOE,Plot_ID,type 


columns in workingfile.csv:



JULIAN_DAYS,HP_12CH4,Day_of_exp,HP_13CH4,HP_Delta_iCH4_30s,12CO2,13CO2,CO2_tot,CH4_tot,Delta_30s_iCO2


For each line of record.csv, I want to select the lines from workingfile.csv for which Day_of_exp is contained between DOE and DOE + measuringtime. And make a new file with these selected lines and the columns from record.csv



I have a lot of data, reason why I decided to try python for this.



Thank you very much if anyone can help! I tried several things that did not lead to anywhere...










share|improve this question









New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    DOE + measuringtime, what is measuringtime?
    – Mayank Porwal
    Nov 21 at 12:44










  • Sorry, missed this. measuringtime is just a value that is added to DOE (DOE is the start of the measurement, DOE+measuringtime is the end of the measurement.
    – Lours
    Nov 21 at 13:47















up vote
0
down vote

favorite












I am very new to python and I am now stuck.



I am trying to match my field notes with the data file from my device.



I have two files record.csv and workingfile.csv.

columns in record.csv:



DOE,Plot_ID,type 


columns in workingfile.csv:



JULIAN_DAYS,HP_12CH4,Day_of_exp,HP_13CH4,HP_Delta_iCH4_30s,12CO2,13CO2,CO2_tot,CH4_tot,Delta_30s_iCO2


For each line of record.csv, I want to select the lines from workingfile.csv for which Day_of_exp is contained between DOE and DOE + measuringtime. And make a new file with these selected lines and the columns from record.csv



I have a lot of data, reason why I decided to try python for this.



Thank you very much if anyone can help! I tried several things that did not lead to anywhere...










share|improve this question









New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    DOE + measuringtime, what is measuringtime?
    – Mayank Porwal
    Nov 21 at 12:44










  • Sorry, missed this. measuringtime is just a value that is added to DOE (DOE is the start of the measurement, DOE+measuringtime is the end of the measurement.
    – Lours
    Nov 21 at 13:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am very new to python and I am now stuck.



I am trying to match my field notes with the data file from my device.



I have two files record.csv and workingfile.csv.

columns in record.csv:



DOE,Plot_ID,type 


columns in workingfile.csv:



JULIAN_DAYS,HP_12CH4,Day_of_exp,HP_13CH4,HP_Delta_iCH4_30s,12CO2,13CO2,CO2_tot,CH4_tot,Delta_30s_iCO2


For each line of record.csv, I want to select the lines from workingfile.csv for which Day_of_exp is contained between DOE and DOE + measuringtime. And make a new file with these selected lines and the columns from record.csv



I have a lot of data, reason why I decided to try python for this.



Thank you very much if anyone can help! I tried several things that did not lead to anywhere...










share|improve this question









New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am very new to python and I am now stuck.



I am trying to match my field notes with the data file from my device.



I have two files record.csv and workingfile.csv.

columns in record.csv:



DOE,Plot_ID,type 


columns in workingfile.csv:



JULIAN_DAYS,HP_12CH4,Day_of_exp,HP_13CH4,HP_Delta_iCH4_30s,12CO2,13CO2,CO2_tot,CH4_tot,Delta_30s_iCO2


For each line of record.csv, I want to select the lines from workingfile.csv for which Day_of_exp is contained between DOE and DOE + measuringtime. And make a new file with these selected lines and the columns from record.csv



I have a lot of data, reason why I decided to try python for this.



Thank you very much if anyone can help! I tried several things that did not lead to anywhere...







python csv






share|improve this question









New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 21 at 13:05









Ali AzG

532414




532414






New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 21 at 12:40









Lours

1




1




New contributor




Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    DOE + measuringtime, what is measuringtime?
    – Mayank Porwal
    Nov 21 at 12:44










  • Sorry, missed this. measuringtime is just a value that is added to DOE (DOE is the start of the measurement, DOE+measuringtime is the end of the measurement.
    – Lours
    Nov 21 at 13:47














  • 1




    DOE + measuringtime, what is measuringtime?
    – Mayank Porwal
    Nov 21 at 12:44










  • Sorry, missed this. measuringtime is just a value that is added to DOE (DOE is the start of the measurement, DOE+measuringtime is the end of the measurement.
    – Lours
    Nov 21 at 13:47








1




1




DOE + measuringtime, what is measuringtime?
– Mayank Porwal
Nov 21 at 12:44




DOE + measuringtime, what is measuringtime?
– Mayank Porwal
Nov 21 at 12:44












Sorry, missed this. measuringtime is just a value that is added to DOE (DOE is the start of the measurement, DOE+measuringtime is the end of the measurement.
– Lours
Nov 21 at 13:47




Sorry, missed this. measuringtime is just a value that is added to DOE (DOE is the start of the measurement, DOE+measuringtime is the end of the measurement.
– Lours
Nov 21 at 13:47












2 Answers
2






active

oldest

votes

















up vote
0
down vote













I don't understand what is measuringtime, but you can try with:



import csv

f1 = open("record.csv", "r")
reader = csv.DictReader(f1, delimiter='t') #DictReader let you read the csv header

f2 = open("workingfile.csv", "r")
reader2 = csv.DictReader(f2, delimiter='t')

f3 = csv.writer(open("newfile.csv", "w"))

record = #create a list to append values from first file
workingfile = #create a list to append values from second file

for row in reader:
record.append(row['DOE']) #take values from DOE column
print(values)


for row in reader2:
workingfile.append(row['Day_of_exp']) #take values from Day_of_exp column
print(values2)

for v in workingfile:
if v in record:
f3.writerow([v])
print(v)

f1.close()
f2.close()


This works to compare two columns in different csv.






share|improve this answer




























    up vote
    0
    down vote













    Well, I managed to do something! I share it here just in case. It is probably not the best way to do it, feel free to criticise.



    working_file = "workingfile.csv"
    record_file = "record.csv"
    output = "output.csv"
    measure_t = 0.004167 #in days, 0.004167 day corresponds to a 6 minutes measurement


    out = csv.writer(open(output, "w"))
    out.writerow(["DOE", "Plot_ID", "type", "CO2", "CH4"])

    data1 = pd.read_csv(working_file)
    data2 = pd.read_csv(record_file)

    DOE1 = data1.Day_of_exp.tolist()
    DOE2 = data2.DOE.tolist()
    ID = data2.Plot_ID.tolist()
    typ = data2.type.tolist()
    CO2 = data1.CO2_tot.tolist()
    CH4 = data1.CH4_tot.tolist()

    for x in DOE2:
    for v in DOE1:
    if x <= v <= x + measure_t:
    n = DOE2.index(x)
    y = ID[n]
    z = typ[n]
    co2 = CO2[n]
    ch4 = CH4[n]
    out.writerow((v,y,z,co2,ch4))


    Cheers!






    share|improve this answer








    New contributor




    Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















      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
      });


      }
      });






      Lours is a new contributor. Be nice, and check out our Code of Conduct.










       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412246%2fusing-datas-from-a-csv-file-to-select-data-in-another-csv%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








      up vote
      0
      down vote













      I don't understand what is measuringtime, but you can try with:



      import csv

      f1 = open("record.csv", "r")
      reader = csv.DictReader(f1, delimiter='t') #DictReader let you read the csv header

      f2 = open("workingfile.csv", "r")
      reader2 = csv.DictReader(f2, delimiter='t')

      f3 = csv.writer(open("newfile.csv", "w"))

      record = #create a list to append values from first file
      workingfile = #create a list to append values from second file

      for row in reader:
      record.append(row['DOE']) #take values from DOE column
      print(values)


      for row in reader2:
      workingfile.append(row['Day_of_exp']) #take values from Day_of_exp column
      print(values2)

      for v in workingfile:
      if v in record:
      f3.writerow([v])
      print(v)

      f1.close()
      f2.close()


      This works to compare two columns in different csv.






      share|improve this answer

























        up vote
        0
        down vote













        I don't understand what is measuringtime, but you can try with:



        import csv

        f1 = open("record.csv", "r")
        reader = csv.DictReader(f1, delimiter='t') #DictReader let you read the csv header

        f2 = open("workingfile.csv", "r")
        reader2 = csv.DictReader(f2, delimiter='t')

        f3 = csv.writer(open("newfile.csv", "w"))

        record = #create a list to append values from first file
        workingfile = #create a list to append values from second file

        for row in reader:
        record.append(row['DOE']) #take values from DOE column
        print(values)


        for row in reader2:
        workingfile.append(row['Day_of_exp']) #take values from Day_of_exp column
        print(values2)

        for v in workingfile:
        if v in record:
        f3.writerow([v])
        print(v)

        f1.close()
        f2.close()


        This works to compare two columns in different csv.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          I don't understand what is measuringtime, but you can try with:



          import csv

          f1 = open("record.csv", "r")
          reader = csv.DictReader(f1, delimiter='t') #DictReader let you read the csv header

          f2 = open("workingfile.csv", "r")
          reader2 = csv.DictReader(f2, delimiter='t')

          f3 = csv.writer(open("newfile.csv", "w"))

          record = #create a list to append values from first file
          workingfile = #create a list to append values from second file

          for row in reader:
          record.append(row['DOE']) #take values from DOE column
          print(values)


          for row in reader2:
          workingfile.append(row['Day_of_exp']) #take values from Day_of_exp column
          print(values2)

          for v in workingfile:
          if v in record:
          f3.writerow([v])
          print(v)

          f1.close()
          f2.close()


          This works to compare two columns in different csv.






          share|improve this answer












          I don't understand what is measuringtime, but you can try with:



          import csv

          f1 = open("record.csv", "r")
          reader = csv.DictReader(f1, delimiter='t') #DictReader let you read the csv header

          f2 = open("workingfile.csv", "r")
          reader2 = csv.DictReader(f2, delimiter='t')

          f3 = csv.writer(open("newfile.csv", "w"))

          record = #create a list to append values from first file
          workingfile = #create a list to append values from second file

          for row in reader:
          record.append(row['DOE']) #take values from DOE column
          print(values)


          for row in reader2:
          workingfile.append(row['Day_of_exp']) #take values from Day_of_exp column
          print(values2)

          for v in workingfile:
          if v in record:
          f3.writerow([v])
          print(v)

          f1.close()
          f2.close()


          This works to compare two columns in different csv.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 13:17









          Lara M.

          396317




          396317
























              up vote
              0
              down vote













              Well, I managed to do something! I share it here just in case. It is probably not the best way to do it, feel free to criticise.



              working_file = "workingfile.csv"
              record_file = "record.csv"
              output = "output.csv"
              measure_t = 0.004167 #in days, 0.004167 day corresponds to a 6 minutes measurement


              out = csv.writer(open(output, "w"))
              out.writerow(["DOE", "Plot_ID", "type", "CO2", "CH4"])

              data1 = pd.read_csv(working_file)
              data2 = pd.read_csv(record_file)

              DOE1 = data1.Day_of_exp.tolist()
              DOE2 = data2.DOE.tolist()
              ID = data2.Plot_ID.tolist()
              typ = data2.type.tolist()
              CO2 = data1.CO2_tot.tolist()
              CH4 = data1.CH4_tot.tolist()

              for x in DOE2:
              for v in DOE1:
              if x <= v <= x + measure_t:
              n = DOE2.index(x)
              y = ID[n]
              z = typ[n]
              co2 = CO2[n]
              ch4 = CH4[n]
              out.writerow((v,y,z,co2,ch4))


              Cheers!






              share|improve this answer








              New contributor




              Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






















                up vote
                0
                down vote













                Well, I managed to do something! I share it here just in case. It is probably not the best way to do it, feel free to criticise.



                working_file = "workingfile.csv"
                record_file = "record.csv"
                output = "output.csv"
                measure_t = 0.004167 #in days, 0.004167 day corresponds to a 6 minutes measurement


                out = csv.writer(open(output, "w"))
                out.writerow(["DOE", "Plot_ID", "type", "CO2", "CH4"])

                data1 = pd.read_csv(working_file)
                data2 = pd.read_csv(record_file)

                DOE1 = data1.Day_of_exp.tolist()
                DOE2 = data2.DOE.tolist()
                ID = data2.Plot_ID.tolist()
                typ = data2.type.tolist()
                CO2 = data1.CO2_tot.tolist()
                CH4 = data1.CH4_tot.tolist()

                for x in DOE2:
                for v in DOE1:
                if x <= v <= x + measure_t:
                n = DOE2.index(x)
                y = ID[n]
                z = typ[n]
                co2 = CO2[n]
                ch4 = CH4[n]
                out.writerow((v,y,z,co2,ch4))


                Cheers!






                share|improve this answer








                New contributor




                Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Well, I managed to do something! I share it here just in case. It is probably not the best way to do it, feel free to criticise.



                  working_file = "workingfile.csv"
                  record_file = "record.csv"
                  output = "output.csv"
                  measure_t = 0.004167 #in days, 0.004167 day corresponds to a 6 minutes measurement


                  out = csv.writer(open(output, "w"))
                  out.writerow(["DOE", "Plot_ID", "type", "CO2", "CH4"])

                  data1 = pd.read_csv(working_file)
                  data2 = pd.read_csv(record_file)

                  DOE1 = data1.Day_of_exp.tolist()
                  DOE2 = data2.DOE.tolist()
                  ID = data2.Plot_ID.tolist()
                  typ = data2.type.tolist()
                  CO2 = data1.CO2_tot.tolist()
                  CH4 = data1.CH4_tot.tolist()

                  for x in DOE2:
                  for v in DOE1:
                  if x <= v <= x + measure_t:
                  n = DOE2.index(x)
                  y = ID[n]
                  z = typ[n]
                  co2 = CO2[n]
                  ch4 = CH4[n]
                  out.writerow((v,y,z,co2,ch4))


                  Cheers!






                  share|improve this answer








                  New contributor




                  Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  Well, I managed to do something! I share it here just in case. It is probably not the best way to do it, feel free to criticise.



                  working_file = "workingfile.csv"
                  record_file = "record.csv"
                  output = "output.csv"
                  measure_t = 0.004167 #in days, 0.004167 day corresponds to a 6 minutes measurement


                  out = csv.writer(open(output, "w"))
                  out.writerow(["DOE", "Plot_ID", "type", "CO2", "CH4"])

                  data1 = pd.read_csv(working_file)
                  data2 = pd.read_csv(record_file)

                  DOE1 = data1.Day_of_exp.tolist()
                  DOE2 = data2.DOE.tolist()
                  ID = data2.Plot_ID.tolist()
                  typ = data2.type.tolist()
                  CO2 = data1.CO2_tot.tolist()
                  CH4 = data1.CH4_tot.tolist()

                  for x in DOE2:
                  for v in DOE1:
                  if x <= v <= x + measure_t:
                  n = DOE2.index(x)
                  y = ID[n]
                  z = typ[n]
                  co2 = CO2[n]
                  ch4 = CH4[n]
                  out.writerow((v,y,z,co2,ch4))


                  Cheers!







                  share|improve this answer








                  New contributor




                  Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered Nov 21 at 17:10









                  Lours

                  1




                  1




                  New contributor




                  Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  Lours is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






















                      Lours is a new contributor. Be nice, and check out our Code of Conduct.










                       

                      draft saved


                      draft discarded


















                      Lours is a new contributor. Be nice, and check out our Code of Conduct.













                      Lours is a new contributor. Be nice, and check out our Code of Conduct.












                      Lours is a new contributor. Be nice, and check out our Code of Conduct.















                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412246%2fusing-datas-from-a-csv-file-to-select-data-in-another-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

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                      Calculate evaluation metrics using cross_val_predict sklearn

                      Insert data from modal to MySQL (multiple modal on website)