13

I have a dataframe with several columns, and I want to append to an empty list the values of one column, so that the desired output would be the following:

empty_list = [value_1,value_2,value_3...]

I have tried the following:

df = pd.DataFrame({'country':['a','b','c','d'],
      'gdp':[1,2,3,4],
      'iso':['x','y','z','w']})
a_list = []

a_list.append(df['iso'])
a_list.append(df['iso'].values)
a_list.append(df['iso'].tolist())

Either way, I get a list with lists, numpy arrays or series inside it, and I would like to have directly the records.

Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41

5 Answers5

22

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']
jeppoo1
  • 650
  • 1
  • 10
  • 23
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
3

extend does what you ask for . If you try do this with append, you can do something like:

import itertools
a_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']
anky
  • 74,114
  • 11
  • 41
  • 70
2

Your problem arises from the fact that df['iso'].tolist() creates a list. The list is appended (given a place in the list at the single index), so you get a list of list. You can try:

a_list.extend(df['iso'].tolist())
balkon16
  • 1,338
  • 4
  • 20
  • 40
1

To access the data of each row of the Pandas dataframe we can use DataFrame.iat attribute and then we can append the data of each row to the end of the list. In first for loop iterate over each row and create a list to store the data of the current row In second for loop iterate over all the columns and append the data of each column to the list after that append the current row to the list

df = pd.DataFrame({'country':['a','b','c','d'],'gdp':[1,2,3,4],'iso':['x','y','z','w']})
a_list = []
for i in range((df.shape[0])):
cur_row =[]
for j in range(df.shape[1]):
    cur_row.append(df.iat[i, j])            
a_list.append(cur_row) 
Santosh
  • 54
  • 6
  • Please edit your answer and add the explanation to fullfill stackoverflows guidelines for good answers – Flo May 27 '19 at 09:27
0

This example should be enough:

myList = df['iso'].tolist() 
print(myList)

Output:

['x', 'y', 'z', 'w']

tuomastik
  • 4,559
  • 5
  • 36
  • 48
SimbaPK
  • 566
  • 1
  • 7
  • 26