2

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...

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Sarah
  • 627
  • 7
  • 25

2 Answers2

2

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

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

Lewis
  • 2,718
  • 1
  • 11
  • 28
  • 1
    [`df.values`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.values.html) is not recommended to use it's mentioned in the docs too. Instead use `df.iloc[0].to_numpy()` – Ch3steR Jun 27 '20 at 19:24
  • I'm getting a key error ```KeyError: 0``` when doing this.. – Sarah Jun 27 '20 at 19:27
1

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

Sam
  • 475
  • 1
  • 7
  • 19
  • You can read more about [`"chained assignment"` here](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#why-does-assignment-fail-when-using-chained-indexing) and check this [related question too](https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas) – Ch3steR Jun 27 '20 at 19:32
  • @Ch3steR OP stated that it's row 0 hence it is `iloc`, `to_list` is just alias for `tolist`, and `df.iloc[0]['b', 'c']` should be `df.iloc[0][['b', 'c']]`, you mind explain why it's not recommedned? – Sam Jun 27 '20 at 19:33
  • Mentioned the links in the comment above and I never mentioned anything about `to_list`/`tolist` I know it's an alias. Yes, I understand it was misleading(Deleted it). ;) – Ch3steR Jun 27 '20 at 19:35
  • The correct way to do it is `df.iloc[0, ['b', 'c']]` instead of `df.iloc[0]['b', 'c']`(Not recommended). – Ch3steR Jun 27 '20 at 19:37
  • The link you shared is for assigning new values into the dataframe, in the link it showed chained assignment sometimes do not work correctly as part of the chain can return a copy so the original df is not modified, but the question OP asking here is to just access the value not assigning. – Sam Jun 27 '20 at 19:38
  • Yes, I know beginners might not know this they use chained assignment, so it's the common standard norm in pandas to use `df.iloc[0, ['b', 'c']]` and not chaining. And I'm **not** saying your answer is bad just wanted to propagate a safer/common coding way. – Ch3steR Jun 27 '20 at 19:42
  • The chained assignment is a bad way of writing when you are doing assignment, and that somehow also translates to accessing values? Plus, OP wants row 0, not index whose value is 0. But it's good to point out the caveat of using chained assignment. – Sam Jun 27 '20 at 19:46
  • Check this [answer](https://stackoverflow.com/q/41253170/12416453) and [this](https://stackoverflow.com/questions/56774184/how-and-when-to-use-chain-indexing-in-python-pandas) and it's mentioned in the [docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html) too. Under warning saying *Whether a copy or a reference is returned for a setting operation, may depend on the context. This is sometimes called chained assignment and should be avoided.* I'm just saying chained indexing is generally avoided. – Ch3steR Jun 27 '20 at 19:51
  • Check [medium blog by Ted Petrou](https://medium.com/dunder-data/selecting-subsets-of-data-in-pandas-part-4-c4216f84d388#:~:text=The%20major%20problem%20with%20chained,older%20than%2010%20to%2099.) check *Identifying chained indexing* and *heading making examples idiomatic*. While contributing to pandas v1.1.0 pandas core dev jef reback , tom Augspurger and Marco Gorelli were my code reviewers and they explicitly mentioned not to use chained indexing even when you are not assigning( just because its a safer way and considered the standard norm of doing things and fewer calls to `__getitem__`) – Ch3steR Jun 27 '20 at 20:08
  • `But it's good to point out the caveat of using chained assignment.` Yes, agreed. It's worth adding it to the answer and OP can use whichever fits his use-case the best. ;) – Ch3steR Jun 27 '20 at 20:13