Convert one DataFrame row to flat list

You get a nested list because you select a sub data frame.

This takes a row, which can be converted to a list without flattening:

df.loc[0, :].values.tolist()
[1, 9, 'a']

How about slicing the list:

df_note.values.tolist()[0]
[4, 6]

The values are stored in an NumPy array. So you do not convert them. Pandas uses a lot of NumPy under the hood. The attribute access df_note.values is just a different name for part of the data frame.


You are almost there, actually just use flatten instead of reduce to unnest the array (instead of unnesting the list), and chain operations to have a one liner:

df.loc[df.n == "d", ['a','b']].values.flatten().tolist()
#[4, 6]

Tags:

Python

Pandas