How can I change values in a data frame into a list of values?

Viewed 90

I'm using Plotly to create a pie chart, and I need to get values for the pie chart. So, for example, I have a dataframe looks like this:

df = pd.DataFrame(np.array([[1, 2, 3]]),
                   columns=['a', 'b', 'c'])

And it will give something like:

  a b c
0 1 2 3

I'm trying to get the values of row 0 from b to c, which it should be:

[2, 3]

How can I achieve this? I tried so far:

df[['b', 'c']].values.tolist()

And it gives:

[[2, 3]]

And above does not work for the Plotly values...

2 Answers

Locate the index label, extract the values and convert to list.

Like so: df.loc[0, ['b', 'c']].tolist()

df.iloc[0][['b', 'c']].to_list() or you can just call list on the selected series list(df.iloc[0][['b', 'c']])

Related