panda, column access

Andrew Zyman formisc at gmail.com
Mon Feb 27 15:09:13 EST 2017


Hello,
 i'm trying to understand what is the difference between the below code. 
And how do i access the column's data without hardcoding the column name in such a way that i'll be able to convert it's values into the list of strings?

I appreciate your help.

Short version:
colnames='hostname'
data = pandas.read_csv(input_file, names=colnames)


list_data = data.hostname.tolist() or list_data = data['hostname'].tolist()

returns the list like - ['hostname','hostname1'] - exactly what i want.


but neither of the below versions work as expected (returning the list of strings):

list_data = data[0].tolist() -> errors with "return self._getitem_column(key)"

list_data = data[colnames].values.tolist() -> the list_data is : [ ['hostname'],['hostname1'...] 

list_data = data.tolist() -> errors with "AttributeError: 'DataFrame' object has no attribute 'tolist'"



More details:

[code]

file ./Data/domain.csv
./domain.csv: ASCII text, with CRLF line terminators

head -3 ./Data/domain.csv 
sunqq.ux.yyy.zzzz
sunww.ux.yyy.zzzz
sunee.ux.yyy.zzzz

[/code]

all work is done in Python 2.7. 
Working version
[code]
<skip>

input_file = r'Data/2domain.csv'
colnames = ['hostname']
data = pandas.read_csv(input_file, names=colnames)
list_data = data.hostname.tolist()
list_data = list(set(list_data))

print list_data

[/code]

Non working version:
[code]

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

# neither
list_data = data[0].tolist()
list_data = data[colnames].tolist()
list_data = data['QUERY'].tolist()
list_data = data.tolist()
# return the list I expected

list_data = list(set(list_data))
print list_data

[/code]




More information about the Python-list mailing list