formatting row output from python pandas dataframe iterrows()

Viewed 556

I've got a python pandas dataframe (my_df). I'd like to extract the rows using iterrows(), then turn the rows into lists, and finally append the rows-turned-lists to a list of lists (my_list).

import pandas as pd

# DATA 
data = {'a': [8, 8, 8, 7],
    'b': [7, 8, 8, 8],
    'c': [7, 7, 8, 8],
    'd': [7, 7, 7, 7]
    }
my_df = pd.DataFrame(data, columns = ['a', 'b', 'c', 'd'])

print(my_df)
   a  b  c  d
0  8  7  7  7
1  8  8  7  7
2  8  8  8  7
3  7  8  8  7

The problem's the format I get from row in iterrows(), because in addition to the bare data I'm after it also includes additional information. How do I strip the additional information, so that I get my desired output (see code below)?

Thanks!

# CODE 
my_list = []
for index, row in my_df.iterrows():
    my_list.append([row])


# OUTPUT
print(my_list)
[[a    8
b    7
c    7
d    7
Name: 0, dtype: int64], [a    8
b    8
c    7
d    7
Name: 1, dtype: int64], [a    8
b    8
c    8
d    7
Name: 2, dtype: int64], [a    7
b    8
c    8
d    7
Name: 3, dtype: int64]]


# DESIRE OUTPUT 
[[8, 7, 7, 7], [8, 8, 7, 7], [8, 8, 8, 7], [7, 8, 8, 7]]
3 Answers

Just use tolist:

my_list = []
for index, row in my_df.iterrows():
    my_list.append(row.tolist())
print(my_list)

Output:

[[8, 7, 7, 7], [8, 8, 7, 7], [8, 8, 8, 7], [7, 8, 8, 7]]

What you do is highly inefficient, just use the underlying numpy array:

my_df.values.tolist()

output:

[[8, 7, 7, 7], [8, 8, 7, 7], [8, 8, 8, 7], [7, 8, 8, 7]]

Check itertuples method, it gives namedtuple for each row, which might then easily converted to list

import pandas as pd

# DATA 
data = {'a': [8, 8, 8, 7],
    'b': [7, 8, 8, 8],
    'c': [7, 7, 8, 8],
    'd': [7, 7, 7, 7]
    }
my_df = pd.DataFrame(data, columns = ['a', 'b', 'c', 'd'])
my_list = [list(i) for i in my_df.itertuples()]
print(my_list)

output

[[0, 8, 7, 7, 7], [1, 8, 8, 7, 7], [2, 8, 8, 8, 7], [3, 7, 8, 8, 7]]
Related