Colin O'Brien
Colin O'Brien

Reputation: 2325

How to get a pandas dataframe to a .tde?

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.

EDIT: So at the moment I'm trying to loop through each column, row by row and add each specific cell. For some reason I get thrown this error.

colnames = all_data.columns

for r in range(0, len(all_data)):
newrow = tde.Row(tableDef)
for c in range(0, len(colnames)):
    if str(colnames[c]) == 'Run Date':
        newrow.setCharString(c, all_data.iloc[r,c])
    elif str(colnames[c]) == 'Sales Order':
        newrow.setCharString(c, all_data.iloc[r,c])  
    else:
        newrow.setNull(c)

table.insert(newrow)

TypeError                                 Traceback (most recent call last)
<ipython-input-27-db8a798ec66b> in <module>()
      5             newrow.setCharString(c, all_data.iloc[r,c])
      6         elif str(colnames[c]) == 'Sales Order':
----> 7             newrow.setCharString(c, all_data.iloc[r,c])
      8         else:
      9             newrow.setNull(c)

    C:\Anaconda\lib\site-packages\dataextract\Base.pyc in setCharString(self, columnNumber, value)
    335             self._handle
    336           , c_int(columnNumber)
--> 337           , c_char_p(value)
    338         )
    339 

TypeError: string or integer address expected instead of numpy.int64 instance

Upvotes: 2

Views: 1957

Answers (2)

philshem
philshem

Reputation: 25381

Based on your traceback, the problem is that you are trying to insert a numpy.int64 instead of a "regular" integer.

For example, to convert to a regular integer before inserting, use the int() function:

int(numpyint)

Other datatypes will also need to be converted, so use the str(), unicode() or float() functions.

source

Upvotes: 0

philshem
philshem

Reputation: 25381

Tableau provides an API for creating data extract (TDE) files - DETAILS and DOWNLOAD

With the Python drivers for the API, you can easily convert a dataframe.

See, for example, this post in how to turn your data into the TDE format

Upvotes: 2

Related Questions