I dont think the problem is with the syntax, your Dataframe just does not contain the index you are looking for.
For me this works:
In [1]: data = pd.DataFrame({0:[1,2,3], 1:[4,5,6], 2:[7,8,9]})
In [2]: data[[0]]
Out[2]:
0
0 1
1 2
2 3
I think what confuses you about the [[0]]
syntax is that the squared brackets are used in python for two completely different things, and the [[0]]
statement uses both:
A. []
is used to create a list. In the above example [0]
creates a list with the single element 0
.
B. []
is also used to access an element from a list (or dict,...). So data[0]
returns the 0.-th element of data
.
The next confusion thing is that while the usual python lists are indexed by numbers (eg. data[4]
is the 4. element of data
), Pandas Dataframes can be indexed by lists. This is syntactic sugar to easily access multiple columns of the dataframe at once.
So in my example from above, to get column 0
and 1
you can do:
In [3]: data[[0, 1]]
Out[3]:
0 1
0 1 4
1 2 5
2 3 6
Here the inner [0, 1]
creates a list with the elements 0
and 1
. The outer [ ]
retrieve the columns of the dataframe by using the inner list as an index.
For more readability look at this, its the exact same:
In [4]: l = [0, 1]
In [5]: data[l]
Out[5]:
0 1
0 1 4
1 2 5
2 3 6
If you only want the first column (column 0
) you get this:
In [6]: data[[0]]
Out[6]:
0
0 1
1 2
2 3
Which is exactly what you were looking for.