Skip to main content

I'm working with a Pandas dataframe.

I have all of my data loaded and all of the manipulations I would like to perform, done.

I'm looking for the specific lines of code which can take this dataframe and copy the rows to a table which I have defined as part of a .tde extract.

If anyone has experience with this I'd love to see what you wrote. There are examples online for using a .csvreader with python which loops through the rows and adds them as they are being read but I can't find any example of how to take data stored in a dataframe and apply it to a table defined as part of an extract.

8 个回答
  1. 2016年2月1日 18:24

    Colin, I know this is older did you ever get your answer on this?

     

    I ran it across it doing research and I have solved this using Pandas. 

     

    I followed along the API instructions to create a TDE from Tableau then used a DataFrame to populate the data in a loop reading through all the rows.  I pasted a sample Python script I wrote below.  Using some dummy data I created the TDE file.

     

    I am sure there are other ways to make this more efficient but this worked for me.

     

    Regards,

     

    Frank

     

    import dataextract as tde

    import os

    import pandas as pd

    import numpy as np

    import datetime as dt

     

    try:

        tdefile = tde.Extract('data.tde')

    except:

        os.remove('data.tde')

        tdefile = tde.Extract('data.tde')

     

    ⌗tdefile = tde.Extract('data.tde')

    tableDef = tde.TableDefinition()

    tableDef.addColumn('ID', tde.Type.CHAR_STRING) #0

    tableDef.addColumn('Name', tde.Type.CHAR_STRING) #1

    tableDef.addColumn('Age', tde.Type.INTEGER) # 2

    table = tdefile.addTable('Extract', tableDef)

    newrow = tde.Row(tableDef)

     

    data = pd.read_excel('data.xlsx')

    for i in range(0,data.shape[0]):

        newrow.setCharString(0, str(data.ix[i,0]))

        newrow.setCharString(1, str(data.ix[i,1]))

        newrow.setInteger(2, int(data.ix[i,2]))

        table.insert(newrow)

     

    tdefile.close()

0/9000