text: user15596: I have the following file in tab separated values (TSV).
  
  
  
  
  ID
  Taxonomy
  
  
  
  
  76980f6d906c6baaef3f96559fc7ba1b
  d__Eukaryota; p__Cercozoa; c__Vampyrellidae; o__Vampyrellidae; f__Vampyrellidae; g__uncultured
  
  
  4f808d3adec894395304661391037369
  d__Eukaryota; p__Ochrophyta; c__Xanthophyceae; o__uncultured; f__uncultured; g__uncultured
  
  
  777584465097568f5b30bc9e2b6584c1
  d__Eukaryota; p__Labyrinthulomycetes; c__uncultured; o__uncultured; f__uncultured; g__uncultured
  
  
  5a99dafabe78847d8f5fed0d37f8672a
  d__Eukaryota; p__Labyrinthulomycetes; c__Labyrinthulomycetes; o__Labyrinthulomycetes; f__Thraustochytriaceae; g__uncultured
  
  
  
  
  I would like to modify the "g__uncultured" with the last known rank possibly using either shell or R scripts.
  The new table would then read:
  
  
  
  
  ID
  Taxonomy
  
  
  
  
  76980f6d906c6baaef3f96559fc7ba1b
  d__Eukaryota; p__Cercozoa; c__Vampyrellidae; o__Vampyrellidae; f__Vampyrellidae; g__f__Vampyrellidae
  
  
  4f808d3adec894395304661391037369
  d__Eukaryota; p__Ochrophyta; c__Xanthophyceae; o__uncultured; f__uncultured; g__c__Xanthophyceae
  
  
  777584465097568f5b30bc9e2b6584c1
  d__Eukaryota; p__Labyrinthulomycetes; c__uncultured; o__uncultured; f__uncultured; g__p__Labyrinthulomycetes
  
  
  5a99dafabe78847d8f5fed0d37f8672a
  d__Eukaryota; p__Labyrinthulomycetes; c__Labyrinthulomycetes; o__Labyrinthulomycetes; f__Thraustochytriaceae; g__f__Thraustochytriaceae
  
  
  
  
  user3848: The pandas solution is here:
  #!/usr/bin/env python3
  import pandas as pd
  from pathlib import Path
  
  path = '/Whatever'
  infile = 'myfile.csv'
  outfile = 'outfile.csv'
  pd.set_option('display.max_colwidth', None)
  df = pd.read_csv(Path(path,infile), sep='\t')
  taxaChange = {'76980f6d906c6baaef3f96559fc7ba1b':'g__f__Vampyrellidae', '4f808d3adec894395304661391037369':'g__c__Xanthophyceae'}
  df2 = df.set_index('ID')
  df3 = df2
  for x,y in taxaChange.items():
      df3.loc[x, 'Taxonomy'] = df2.loc[x, 'Taxonomy'].replace('g__uncultured', y)
  print (df3)
  
  df.to_csv(Path(path,outfile), sep='\t')
  
  Output
  ID                                       Taxonomy                                                                                                                                             
  76980f6d906c6baaef3f96559fc7ba1b         d__Eukaryota; p__Cercozoa; c__Vampyrellidae; o__Vampyrellidae; f__Vampyrellidae; g__f__Vampyrellidae
  4f808d3adec894395304661391037369         d__Eukaryota; p__Ochrophyta; c__Xanthophyceae; o__uncultured; f__uncultured; g__c__Xanthophyceae
  777584465097568f5b30bc9e2b6584c1         d__Eukaryota; p__Labyrinthulomycetes; c__uncultured; o__uncultured; f__uncultured; g__uncultured
  5a99dafabe78847d8f5fed0d37f8672a         d__Eukaryota; p__Labyrinthulomycetes; c__Labyrinthulomycetes; o__Labyrinthulomycetes; f__Thraustochytriaceae; g__uncultured
  
  Both g__f__Vampyrellidae and g__c__Xanthophyceae are there, i.e. the code was tested and worked fine.
  The point for the pandas approach is you've a natural table format so it makes sense to take it straight to a dataframe (table -> dataframe). regex-ing using \t and \n is of course the alternative and you need to formulate the table as a hash (Perl) or dictionary (Python).
  Notes The df and df2 can reduced a single dataframe by updating the df (inplace was omitted and will work for an index), rather than creating a new df. However, the df3 is needed (need to copy the dataframe) because 'inplace' didn't work. inplace is a command to force the dataframe to update. The set_option is just so I can print the display without truncation.
  Finally
  You substitute the infile and outfile to correct path and name values, save it as a .py file and just python3 myscript.py.
  
original_text: 15596: <p>I have the following file in tab separated values (TSV).</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">ID</th>
  <th style="text-align: left;">Taxonomy</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">76980f6d906c6baaef3f96559fc7ba1b</td>
  <td style="text-align: left;">d__Eukaryota; p__Cercozoa; c__Vampyrellidae; o__Vampyrellidae; f__Vampyrellidae; g__uncultured</td>
  </tr>
  <tr>
  <td style="text-align: left;">4f808d3adec894395304661391037369</td>
  <td style="text-align: left;">d__Eukaryota; p__Ochrophyta; c__Xanthophyceae; o__uncultured; f__uncultured; g__uncultured</td>
  </tr>
  <tr>
  <td style="text-align: left;">777584465097568f5b30bc9e2b6584c1</td>
  <td style="text-align: left;">d__Eukaryota; p__Labyrinthulomycetes; c__uncultured; o__uncultured; f__uncultured; g__uncultured</td>
  </tr>
  <tr>
  <td style="text-align: left;">5a99dafabe78847d8f5fed0d37f8672a</td>
  <td style="text-align: left;">d__Eukaryota; p__Labyrinthulomycetes; c__Labyrinthulomycetes; o__Labyrinthulomycetes; f__Thraustochytriaceae; g__uncultured</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I would like to modify the &quot;g__uncultured&quot; with the last known rank possibly using either shell or R scripts.</p>
  <p>The new table would then read:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th style="text-align: left;">ID</th>
  <th style="text-align: left;">Taxonomy</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: left;">76980f6d906c6baaef3f96559fc7ba1b</td>
  <td style="text-align: left;">d__Eukaryota; p__Cercozoa; c__Vampyrellidae; o__Vampyrellidae; f__Vampyrellidae; g__f__Vampyrellidae</td>
  </tr>
  <tr>
  <td style="text-align: left;">4f808d3adec894395304661391037369</td>
  <td style="text-align: left;">d__Eukaryota; p__Ochrophyta; c__Xanthophyceae; o__uncultured; f__uncultured; g__c__Xanthophyceae</td>
  </tr>
  <tr>
  <td style="text-align: left;">777584465097568f5b30bc9e2b6584c1</td>
  <td style="text-align: left;">d__Eukaryota; p__Labyrinthulomycetes; c__uncultured; o__uncultured; f__uncultured; g__p__Labyrinthulomycetes</td>
  </tr>
  <tr>
  <td style="text-align: left;">5a99dafabe78847d8f5fed0d37f8672a</td>
  <td style="text-align: left;">d__Eukaryota; p__Labyrinthulomycetes; c__Labyrinthulomycetes; o__Labyrinthulomycetes; f__Thraustochytriaceae; g__f__Thraustochytriaceae</td>
  </tr>
  </tbody>
  </table>
  </div> 3848: <p>The <code>pandas</code> solution is here:</p>
  <pre><code>#!/usr/bin/env python3
  import pandas as pd
  from pathlib import Path
  
  path = '/Whatever'
  infile = 'myfile.csv'
  outfile = 'outfile.csv'
  pd.set_option('display.max_colwidth', None)
  df = pd.read_csv(Path(path,infile), sep='\t')
  taxaChange = {'76980f6d906c6baaef3f96559fc7ba1b':'g__f__Vampyrellidae', '4f808d3adec894395304661391037369':'g__c__Xanthophyceae'}
  df2 = df.set_index('ID')
  df3 = df2
  for x,y in taxaChange.items():
      df3.loc[x, 'Taxonomy'] = df2.loc[x, 'Taxonomy'].replace('g__uncultured', y)
  print (df3)
  
  df.to_csv(Path(path,outfile), sep='\t')
  </code></pre>
  <p>Output</p>
  <pre><code>ID                                       Taxonomy                                                                                                                                             
  76980f6d906c6baaef3f96559fc7ba1b         d__Eukaryota; p__Cercozoa; c__Vampyrellidae; o__Vampyrellidae; f__Vampyrellidae; g__f__Vampyrellidae
  4f808d3adec894395304661391037369         d__Eukaryota; p__Ochrophyta; c__Xanthophyceae; o__uncultured; f__uncultured; g__c__Xanthophyceae
  777584465097568f5b30bc9e2b6584c1         d__Eukaryota; p__Labyrinthulomycetes; c__uncultured; o__uncultured; f__uncultured; g__uncultured
  5a99dafabe78847d8f5fed0d37f8672a         d__Eukaryota; p__Labyrinthulomycetes; c__Labyrinthulomycetes; o__Labyrinthulomycetes; f__Thraustochytriaceae; g__uncultured
  </code></pre>
  <p>Both <code>g__f__Vampyrellidae</code> and <code>g__c__Xanthophyceae</code> are there, i.e. the code was tested and worked fine.</p>
  <p>The point for the <code>pandas</code> approach is you've a natural table format so it makes sense to take it straight to a dataframe (table -&gt; dataframe). regex-ing using <code>\t</code> and <code>\n</code> is of course the alternative and you need to formulate the table as a hash (Perl) or dictionary (Python).</p>
  <p><strong>Notes</strong> The <code>df</code> and <code>df2</code> can reduced a single dataframe by updating the <code>df</code> (<code>inplace</code> was omitted and will work for an index), rather than creating a new <code>df</code>. However, the <code>df3</code> is needed (need to copy the dataframe) because 'inplace' didn't work. <code>inplace</code> is a command to force the dataframe to update. The <code>set_option</code> is just so I can print the display without truncation.</p>
  <p><em>Finally</em>
  You substitute the infile and outfile to correct path and name values, save it as a <code>.py</code> file and just <code>python3 myscript.py</code>.</p>
  