pandas - Python [[0]] meaning -


i running python script (kaggle script). works in 3.4.5 virtualenv, not in 3.5.2

i not sure why , not familiar [[0]] syntax. below snippet.

import pandas pd data = pd.read_csv(r'path\train.csv') labels_flat = data[[0]].values.ravel() 

it should produce list of values csv's first column.

in 3.5.2 error:

keyerror: '[0] not in index' 

i tried replicate value with

labels_flat = [] lf = data.values.tolist() row in lf:     labels_flat.append(row[0]) 

but don't think same thing.

i dont think problem syntax, dataframe not contain index looking for.

for me 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 confuses [[0]] syntax squared brackets used in python 2 different things, , [[0]] statement uses both:

a. [] used create list. in above example [0] creates list single element 0.

b. [] used access element list (or dict,...). data[0] returns 0.-th element of data.

the next confusion thing while usual python lists indexed numbers (eg. data[4] 4. element of data), pandas dataframes can indexed lists. syntactic sugar access multiple columns of dataframe @ once. in example above, column 0 , 1 can do:

in [3]: data[[0, 1]] out[3]:     0  1 0  1  4 1  2  5 2  3  6 

here inner [0, 1] creates list elements 0 , 1. outer [ ] retrieve columns of dataframe using inner list index.

for more readability @ this, 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 want first column (column 0) this:

in [6]: data[[0]] out[6]:     0 0  1 1  2 2  3 

which looking for.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -