Home > Blockchain >  Print row values as lists iteratively without getting the column names
Print row values as lists iteratively without getting the column names

Time:08-03

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 which looks like the following should print out only the rows as lists one after the other:

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.

CodePudding user response:

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']]

CodePudding user response:

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']]
  • Related