Skip to content

Instantly share code, notes, and snippets.

@brentp
Last active December 20, 2015 03:58
Show Gist options
  • Save brentp/6066942 to your computer and use it in GitHub Desktop.
Save brentp/6066942 to your computer and use it in GitHub Desktop.
import pandas as pd
from StringIO import StringIO
import numpy as np
import re
import string
txt = """\
a b c d e f g h i
0 0 1 0 0 1 0 0 0
1 0 0 0 0 0 0 0 0
1 0 0 1 0 1 0 1 0
0 0 0 1 0 1 0 1 0
1 0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0
1 0 1 1 0 0 0 0 0
1 0 1 1 0 0 0 0 1
1 0 1 1 0 1 0 0 1"""
df = pd.read_csv(StringIO(txt), dtype=np.int_, sep="\s+")
df.index = [string.letters[i] for i in range(len(df))]
print "original:", df.shape
df.to_csv('t.txt', sep="\t")
df = pd.read_csv('t.txt', dtype=np.int_, sep="\s+", index_col=0)
print "OK \s+", df.shape
# with index_col, and sep of "\t" get an error about dtype.
df = pd.read_csv('t.txt', dtype=np.int_, sep="\t", index_col=0)
print "OK \t"
a b c d e f g h i
a 0 0 1 0 0 1 0 0 0
b 1 0 0 0 0 0 0 0 0
c 1 0 0 1 0 1 0 1 0
d 0 0 0 1 0 1 0 1 0
e 1 0 1 1 0 0 0 0 0
f 0 0 0 0 0 1 0 0 0
g 1 0 1 1 0 0 0 0 0
h 1 0 1 1 0 0 0 0 1
i 1 0 1 1 0 1 0 0 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment