Print row values as lists iteratively without getting the column names

Question:

I am trying to get rows of a dataframe as separate lists. This list should only contain the values in a certain row for a certain number of rows. For instance, a dataframe that looks like the following should print out only the rows as lists one after the other:

Example dataframe

Name Age Gender
Michael 5 M
Fitz 10 F

should print out

[Michael, 5, M]
[Fitz, 10, F]

I know df.loc[[1]] prints out the first row, but it also prints out the column names. I am trying to get rid of the column names, and have a list of only row values.

Asked By: Eliot Kim

||

Answers:

Depending on what you can, you can join the rows into list with DataFrame.apply or just convert the DataFrame to list of rows

cols = ['Name', 'Age', 'Gender']

out = df[cols].apply(list, axis=1)
# or
lsts = df[cols].values.tolist()
print(out)

0    [Michael, 5, M]
1      [Fitz, 10, F]
dtype: object

print(lsts)

[['Michael', 5, 'M'], ['Fitz', 10, 'F']]
Answered By: Ynjxsjmh

Depending on if it’s a header or regular row you have two options:

# If the first row is a header row.

df.values.tolist()
# If the first row is a regular row. Where the [1:] determines how many of the rows 
# you want to remove.

df.values.tolist()[1:]

Both produce:

[['Michael', 5, 'M'], [10, 'F', 'Fitz']]
Answered By: jsn
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.