On 2/27/17, Andrew Zyman <form...@gmail.com> wrote: > 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'
I think you meant: 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'" What do you want is probably: >>> data[colnames[0]].tolist() or maybe if you don't want to remember colnames: >>> data[data.columns[0]].tolist() Be careful! Type of data[x] depend on what type is x. >>> type(data['hostname']) # -> pandas.core.series.Series >>> type(data[['hostname']]) # -> pandas.core.frame.DataFrame -- https://mail.python.org/mailman/listinfo/python-list