0

Let's say my file contains these data:

t   c   a
a   1   2
b   2   3
c   1   1

In order to select and save only the columns I'm interested in, I followed this post and installed the pandas module. Then I did:

>>> df = pandas.read_csv('test')
>>> df
    t\tc\ta
 0  a\t1\t2
 1  b\t2\t3
 2  c\t1\t1

However, when I type df.a to select column "a" I get this error:

AttributeError: 'DataFrame' object has no attribute 'a'

Also tried df['t'] and got this error instead:

KeyError: u'no item named t'
Community
  • 1
  • 1
HappyPy
  • 9,839
  • 13
  • 46
  • 68

1 Answers1

3

Specify delimiter (default is ,).

>>> import pandas
>>> with open('test.csv', 'w') as f:
...     f.write('t\tc\ta\na\t1\t2\nb\t2\t3\nc\t1\t1\n')
... 

>>> df = pandas.read_csv('test.csv')
>>> df.columns
Index([t    c   a], dtype=object)

>>> df = pandas.read_csv('test.csv', delimiter='\t') # <--
>>> df.columns
Index([t, c, a], dtype=object)

>>> df['t']
0    a
1    b
2    c
Name: t
falsetru
  • 357,413
  • 63
  • 732
  • 636