Export vectors from orgmode to tsv file and import using python pandas

Orgmode tables are handy and powerful. I was copying and pasting few column arrays from a spreadsheet application to orgmode, and I wasn't able to concatenate the vectors to an org-table. So, I assigned the vectors to variables in python and I exported them both in orgmode and to a tsv file.

Decimal representation

#+NAME: decimal
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
| 70 |

Hexadecimal representation

#+NAME: hexadecimal
|  0xA |
| 0x14 |
| 0x1e |
| 0x28 |
| 0x32 |
| 0x3c |
| 0x46 |

Import vectors in python and write to tsv file

The code below is a way to join vectors in a 2D org-table. Also is a demonstration to export tsv using python.

#+BEGIN_SRC python :exports both :var decs=decimal hexs=hexadecimal
import csv
file = open('test', 'w')

data = []

for i in range(0,7):
  data.append([decs[i][0], hexs[i][0]])

# use TAB delimiter to write tsv file
writer = csv.writer(file, delimiter='\t')
for item in data:
    writer.writerow(item)
file.close()

return data
#+END_SRC
#+RESULTS:
| 10 |  0xA |
| 20 | 0x14 |
| 30 | 0x1e |
| 40 | 0x28 |
| 50 | 0x32 |
| 60 | 0x3c |
| 70 | 0x46 |

Pandas to import tsv data

Pandas is a data analysis library in python, with handy functionality.

#+BEGIN_SRC python :exports both
import pandas as pd
bpm = pd.read_csv('test', sep='\t', header=None, names=['Dec', 'Hex'])
return bpm.tail()
#+END_SRC

Now, notice that the leftmost column is an enumeration produced by pandas.

#+RESULTS:
: Dec   Hex
: 2   30  0x1e
: 3   40  0x28
: 4   50  0x32
: 5   60  0x3c
: 6   70  0x46

Latest posts