<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    Hi Paulo,<br>
    I had the same problem some time ago. I am using pandas for a lot of
    things and discovered the quickest way to append a column to a
    vector table is using pandas' .to_sql dataframe method. Here is my
    full function for it:<br>
    <br>
    <pre>import pandas as pa</pre>
    <pre>dataframe = pa.DataFrame( ... )</pre>
    <pre>def appendColumns(vecttbl,dataframe,join=None,layer=1):</pre>
    <pre>    '''Upload a DataFrame (Series should be first converted into a sql compliant datafram)</pre>
    <pre>    to a grass vector table. Join is a valid column in vecttbl or if None take categories.</pre>
    <pre>    Always joins on df.index, which is not uploaded.'''</pre>
    <pre>    # get sqlite.db info</pre>
    <pre>    db = grass.vector_db(vecttbl)[layer]</pre>
    <pre>    con = sqlite3.connect(db['database'])</pre>
    <pre>    # decide join indeces</pre>
    <pre>    itbl = {False:db['key'],True:join}[join!=None]</pre>
    <pre>    # make series to dataframe</pre>
    <pre>    dataframe.to_sql('pandas_temp',con,if_exists='replace',index_label='ix') # index label?</pre>
    <pre>    # join columns to vecttbl</pre>
    <pre>    grass.run_command('v.db.join',map=vecttbl,column=itbl,</pre>
    <pre>                       other_table='pandas_temp',other_column='ix',</pre>
    <pre>                       subset_columns=','.join(map(str,dataframe.columns)))</pre>
    <pre>    # remove temp table</pre>
    <pre>    cur = con.cursor()</pre>
    <pre>    cur.execute('DROP TABLE IF EXISTS pandas_temp')</pre>
    <pre>    con.commit(); con.close()</pre>
    <pre>    return</pre>
    <br>
    The dataframe.to_sql line is essentially all you need, the rest of
    the function is just prepping. <br>
    Hope it helps,<br>
    Michel<br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 10/01/2015 12:05 AM, Paulo van
      Breugel wrote:<br>
    </div>
    <blockquote cite="mid:560C5CA8.9050307@gmail.com" type="cite">
      <meta content="text/html; charset=windows-1252"
        http-equiv="Content-Type">
      <br>
      <br>
      <div class="moz-cite-prefix">On 30-09-15 17:52, Paulo van Breugel
        wrote:<br>
      </div>
      <blockquote cite="mid:560C0520.9030509@gmail.com" type="cite">
        <meta content="text/html; charset=windows-1252"
          http-equiv="Content-Type">
        <br>
        <br>
        <div class="moz-cite-prefix">On 30-09-15 17:47, Anna Petrášová
          wrote:<br>
        </div>
        <blockquote
cite="mid:CAE0EDEoSucig6AYsdar7O1MSpfFwUiJhttSia7aKPXmZf+qpWA@mail.gmail.com"
          type="cite">
          <div dir="ltr"><br>
            <div class="gmail_extra"><br>
              <div class="gmail_quote">On Wed, Sep 30, 2015 at 11:20 AM,
                Paulo van Breugel <span dir="ltr"><<a
                    moz-do-not-send="true"
                    class="moz-txt-link-abbreviated"
                    href="mailto:p.vanbreugel@gmail.com">p.vanbreugel@gmail.com</a>></span>
                wrote:<br>
                <blockquote class="gmail_quote" style="margin:0px 0px
                  0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                  <div text="#000000" bgcolor="#FFFFFF">
                    <div>
                      <div class="h5"> <br>
                        <br>
                        <div>On 30-09-15 16:18, Anna Petrášová wrote:<br>
                        </div>
                        <blockquote type="cite">
                          <div dir="ltr"><br>
                            <div class="gmail_extra"><br>
                              <div class="gmail_quote">On Wed, Sep 30,
                                2015 at 9:27 AM, Paulo van Breugel <span
                                  dir="ltr"><<a
                                    moz-do-not-send="true"
                                    href="mailto:p.vanbreugel@gmail.com"
                                    target="_blank">p.vanbreugel@gmail.com</a>></span>
                                wrote:<br>
                                <blockquote class="gmail_quote"
                                  style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                                  <div dir="ltr"><br>
                                    <div class="gmail_extra"><br>
                                      <div class="gmail_quote"><span>On
                                          Wed, Sep 30, 2015 at 11:53 AM,
                                          Pietro <span dir="ltr"><<a
                                              moz-do-not-send="true"
                                              class="moz-txt-link-abbreviated"
href="mailto:peter.zamb@gmail.com">peter.zamb@gmail.com</a>></span>
                                          wrote:<br>
                                          <blockquote
                                            class="gmail_quote"
                                            style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">On



                                            Wed, Sep 30, 2015 at 9:51
                                            AM, Paulo van Breugel<br>
                                            <span><<a
                                                moz-do-not-send="true"
                                                class="moz-txt-link-abbreviated"
href="mailto:p.vanbreugel@gmail.com">p.vanbreugel@gmail.com</a>>
                                              wrote:<br>
                                              ><br>
                                              ><br>
                                              > On Wed, Sep 30, 2015
                                              at 2:02 AM, Anna Petrášová
                                              <<a
                                                moz-do-not-send="true"
                                                class="moz-txt-link-abbreviated"
href="mailto:kratochanna@gmail.com">kratochanna@gmail.com</a>><br>
                                              > wrote:<br>
                                              >><br>
                                              >><br>
                                              >><br>
                                              >> On Tue, Sep 29,
                                              2015 at 6:09 PM, Paulo van
                                              Breugel<br>
                                              >> <<a
                                                moz-do-not-send="true"
                                                class="moz-txt-link-abbreviated"
href="mailto:p.vanbreugel@gmail.com">p.vanbreugel@gmail.com</a>>
                                              wrote:<br>
                                              >>><br>
                                              >>> This must be
                                              a very basic question, but
                                              I can't find an
                                              easy/direct way<br>
                                              >>> to do this.
                                              In python, if I have an
                                              array with values with a
                                              length equal to<br>
                                              >>> the number of
                                              rows in an attribute table
                                              of a (point) vector layer,
                                              how can<br>
                                              >>> I write those
                                              values to a new column in
                                              that attribute table. I
                                              can of<br>
                                              >>> course first
                                              create the column, but
                                              than how to update that
                                              column with the<br>
                                              >>> values in the
                                              array?<br>
                                              >><br>
                                              >><br>
                                              >> it should be
                                              pretty easy to do with
                                              pygrass, unfortunately
                                              there is no<br>
                                              >> example on
                                              assigning attributes in
                                              the official documentation
                                              [1],  but it<br>
                                              >> should be pretty
                                              easy, something like that
                                              (not tested):<br>
                                              >><br>
                                              >> with
                                              VectorTopo('myvector',
                                              mode='w') as vectormap:<br>
                                              >>     for feature
                                              in vectormap:<br>
                                              >>       
                                               feature.attrs['mycolumn']
                                              = value<br>
                                              >><br>
                                              >><br>
                                              > Thanks, but that
                                              seems to write the vector
                                              back without attribute
                                              table<br>
                                              <br>
                                            </span>You have to save the
                                            changes in the database out
                                            from your cycle, with:<br>
                                            <br>
vectormap.table.conn.commit()<br>
                                          </blockquote>
                                          <div><br>
                                          </div>
                                        </span>
                                        <div>Thanks Pietro. I am,
                                          however, not sure I understand
                                          (I tried to use it, but thanks
                                          to my limited experience in
                                          Python / pygrass not much
                                          luck). Just to be more
                                          specific, I am trying to
                                          create a script that divides
                                          points in training and test
                                          groups, similar to v.kcv, but
                                          with points clustered in
                                          space. E.g.,<br>
                                          <br>
                                          <span
                                            style="font-family:monospace,monospace">#
                                            Create vector<br>
                                            grass.run_command("v.random",

                                            output="testB", npoints=10,
                                            overwrite=True)<br>
                                            grass.run_command("v.db.addtable",


                                            map="testB", columns="X
                                            DOUBLE PRECISION,Y DOUBLE
                                            PRECISION,GR INTEGER")<br>
                                            grass.run_command("v.to.db",
                                            map="test", option="coor",
                                            columns="X,Y")<br>
                                            <br>
                                            # Create groups<br>
                                            vectmap = 'test'<br>
                                            cvals =
                                            array(grass.vector_db_select(vectmap,
                                            layer = int(1), columns =
                                            'X,Y')['values'].values()).astype(np.float)<br>
                                            centroids,_ =
                                            kmeans(cvals,2)<br>
                                            idx,_ = vq(cvals,centroids)<br>
                                            <br>
                                          </span></div>
                                        <div><span
                                            style="font-family:monospace,monospace">#
                                            write results to tabel<br>
                                          </span></div>
                                        <div><span
                                            style="font-family:monospace,monospace">Now



                                            I would like to write idx to
                                            the column 'GR' in the
                                            attribute table of 'test'.<br>
                                          </span><br>
                                        </div>
                                        <div>p.s. I am first creating
                                          the XY columns now, but is
                                          there a function to get the
                                          coordinates (cvals) in pygrass
                                          directly?<br>
                                        </div>
                                        <div><br>
                                          <br>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </blockquote>
                                <div>I don't fully understand the
                                  example, </div>
                              </div>
                            </div>
                          </div>
                        </blockquote>
                        <br>
                      </div>
                    </div>
                    Thanks for the quick response. I basically have a
                    list with values (idx in the example above) which I
                    like to add as a column to the attribute table of an
                    existing vector (point layer). The length of idx is
                    equal to the number of rows in the attribute table.
                    The solution of Anna seems like an elegant solution
                    (and easier and more flexible than other solutions I
                    tried using e.g., sqlite3). However, as I wrote, I
                    end up with a vector without attribute table. You
                    wrote that I "have to save the changes in the
                    database out from your cycle, with:
                    vectormap.table.conn.commit()". I am, however, not
                    sure what you mean with 'out from your cycle' or how
                    to implement that. <br>
                  </div>
                </blockquote>
                <div><br>
                </div>
                <div>probably after the for cycle ends you would call
                  this 'vectormap.table.conn.commit()', if it doesn't
                  works, try to put it in the cycle (I am not sure what
                  is supposed to work).</div>
              </div>
            </div>
          </div>
        </blockquote>
        <br>
        I did try both, with no luck so-far. I'll give it another try
        though, perhaps I did something else wrong.<br>
      </blockquote>
      <br>
      Tried out again, but after running the code below, I end up with
      an empty vector layer (no points and no attribute table). Same if
      I put the vectormap.table.conn.commit() in the for cycle.<br>
      <br>
      <pre>import grass.script as grass</pre>
      <pre>from grass.pygrass.vector import VectorTopo</pre>
      <pre>grass.run_command("v.random", output="testB", npoints=10, overwrite=True)</pre>
      <pre>grass.run_command("v.db.addtable", map="testB", columns="GR INTEGER")</pre>
      <pre>with VectorTopo('testB', mode='w') as vectormap:</pre>
      <pre>    for feature in vectormap:</pre>
      <pre>        feature.attrs['GR'] = 9</pre>
      <pre>        vectormap.table.conn.commit()</pre>
      <br>
      <blockquote cite="mid:560C0520.9030509@gmail.com" type="cite">
        <blockquote
cite="mid:CAE0EDEoSucig6AYsdar7O1MSpfFwUiJhttSia7aKPXmZf+qpWA@mail.gmail.com"
          type="cite">
          <div dir="ltr">
            <div class="gmail_extra">
              <div class="gmail_quote">
                <blockquote class="gmail_quote" style="margin:0px 0px
                  0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                  <div text="#000000" bgcolor="#FFFFFF"><span class="">
                      <br>
                      <blockquote type="cite">
                        <div dir="ltr">
                          <div class="gmail_extra">
                            <div class="gmail_quote">
                              <div>but yes, you can get coordinates:</div>
                              <div><br>
                              </div>
                              <div><font face="monospace, monospace"><span
                                    style="font-size:12.8px">with
                                    VectorTopo('myvector', mode='w') as
                                    vectormap:</span><br
                                    style="font-size:12.8px">
                                  <span style="font-size:12.8px">    for
                                    feature in vectormap:</span><br
                                    style="font-size:12.8px">
                                  <span style="font-size:12.8px">       
                                    print feature.x</span></font><br>
                              </div>
                              <div><span
                                  style="font-family:monospace,monospace;font-size:12.8px"> 
                                        print feature.y</span><font
                                  face="monospace, monospace"><span
                                    style="font-size:12.8px"><br>
                                  </span></font></div>
                            </div>
                          </div>
                        </div>
                      </blockquote>
                      <br>
                    </span> Great, thanks. I did not find this in the
                    manual. If it is not there, perhaps it would be
                    something worth including? I would not mind
                    providing a text, but I am not sure what would be
                    the best way to do that.<br>
                  </div>
                </blockquote>
                <div><br>
                </div>
                <div>Technically, it's in the manual:</div>
                <div><a moz-do-not-send="true"
href="https://grass.osgeo.org/grass70/manuals/libpython/pygrass_vector.html#geometry-classes">https://grass.osgeo.org/grass70/manuals/libpython/pygrass_vector.html#geometry-classes</a></div>
                <div><br>
                </div>
                <div>but we are lacking more examples of often used
                  constructions. Some other examples are available in
                  the recent workshop we did:</div>
                <div><br>
                </div>
                <div><a moz-do-not-send="true"
href="https://github.com/wenzeslaus/python-grass-addon/blob/master/02_pygrass_library.ipynb">https://github.com/wenzeslaus/python-grass-addon/blob/master/02_pygrass_library.ipynb</a><br>
                </div>
              </div>
            </div>
          </div>
        </blockquote>
        <br>
        Thanks, I will have a look at it.<br>
        <blockquote
cite="mid:CAE0EDEoSucig6AYsdar7O1MSpfFwUiJhttSia7aKPXmZf+qpWA@mail.gmail.com"
          type="cite">
          <div dir="ltr">
            <div class="gmail_extra">
              <div class="gmail_quote">
                <div><br>
                </div>
                <blockquote class="gmail_quote" style="margin:0px 0px
                  0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                  <div text="#000000" bgcolor="#FFFFFF"> <br>
                    <blockquote type="cite">
                      <div dir="ltr">
                        <div class="gmail_extra">
                          <div class="gmail_quote">
                            <div> </div>
                            <blockquote class="gmail_quote"
                              style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
                              <div dir="ltr">
                                <div class="gmail_extra">
                                  <div class="gmail_quote">
                                    <div> <br>
                                    </div>
                                    <blockquote class="gmail_quote"
                                      style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span><font
                                          color="#888888"><br>
                                          Pietro<br>
                                        </font></span></blockquote>
                                  </div>
                                  <br>
                                </div>
                              </div>
                            </blockquote>
                          </div>
                          <br>
                        </div>
                      </div>
                    </blockquote>
                    <br>
                  </div>
                </blockquote>
              </div>
              <br>
            </div>
          </div>
        </blockquote>
        <br>
      </blockquote>
      <br>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
grass-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:grass-dev@lists.osgeo.org">grass-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/grass-dev">http://lists.osgeo.org/mailman/listinfo/grass-dev</a></pre>
    </blockquote>
    <br>
  </body>
</html>