Append values from dataframe column to list
You could try this script if you need to append one column only:
a_list = df['iso'].tolist()
For extending a list by appending elements from the iterable, use extend
:
a_list = []a_list.extend(df['iso'].tolist())a_list.extend(df['country'].tolist())print (a_list)['x', 'y', 'z', 'w', 'a', 'b', 'c', 'd']
Another solution is to use numpy.ravel
with transpose:
a_list = df[['iso','country']].values.T.ravel().tolist()print (a_list)['x', 'y', 'z', 'w', 'a', 'b', 'c', 'd']
extend
does what you ask for . If you try do this with append
, you can do something like:
import itertoolsa_list = []a_list.append(df.iso.tolist())a_list.append(df.country.tolist())a_list=list(itertools.chain.from_iterable(a_list))print(a_list)
Output
['x', 'y', 'z', 'w', 'a', 'b', 'c', 'd']