panda, column access

Andrew Zyman formisc at gmail.com
Mon Feb 27 15:50:46 EST 2017


Hello,
 i'd appreciate an explanation about the differences in the code versions below. I'm trying to get the list of the strings out of the column and avoid hardcoding the column name.

cat Data/2domain.csv
hostname
hostname1
hostname2
...
...

Working version(s):
Python2.7:

input_file = r'Data/2domain.csv'
colnames = ['hostname']
data = pandas.read_csv(input_file, names=colnames,header=0)
list_data = data.hostname.tolist()
# or 
#list_data = data['hostname'].tolist()
print list_data


['hostname','hostname1','hostname3'....]


And confusion is with these versions:
colnames = ['hostname']
data = pandas.read_csv(input_file, names=colnames,header=0)

list_data = data[colnames].tolist() 
 -AttributeError: 'DataFrame' object has no attribute 'tolist'

list_data = data[colnames].get_values().tolist() 
 - [ ['hostname'],['hostname1']..]

list_data = data[colnames].get_values()
 - [ ['hostname'],
     ['hostname1']
     ..
   ]




Thank you
AZ



[working code]





[/working code]


[no working code ]

[/not working code ]






More information about the Python-list mailing list