0

If I have a DataFrame with a column showing a set array, how do I get specific values in the set array?

df:

0    Array
1    {878,999,858}

Desired:

1st index: 878
2nd index: 999
3rd index: 858

What I tried:

df['Array'].values[0]
>> {878,999,858}

When I tried to add df['Array'].values[0].index.values[2] to get 2nd index, it doesn't allow me to do so.

Thanks.

5
  • 1
    set is unordered so there is no meaning of index Commented Mar 12, 2021 at 3:46
  • Sets aren't ordered objects, so it doesn't make sense to index them by order. There is no guarantee that placing three items into the set will result in them being shown in the same order. If you just want the elements, you can convert to a list and index into that, but be aware that the order may change. Commented Mar 12, 2021 at 3:47
  • Ordering doesn't really matter at this point, really just trying to reduce the values to 1 items in the set Commented Mar 12, 2021 at 3:48
  • if it is list you can use df['Array'].str[1] for second index Commented Mar 12, 2021 at 3:48
  • Ordering doesn't really matter at this point, then how are you defining 2nd index. There is no 2nd index in set Commented Mar 12, 2021 at 3:49

1 Answer 1

1

It might have better ways to do it but...

Creating test dataframe

>>> import pandas as pd
>>> df = pd.DataFrame({"A": [{878,999,858}]})

Printing dataframe

>>> df
                 A
0  {858, 878, 999}

Printing row 0

>>> df.loc[0]["A"]
{858, 878, 999}

Printing row 0 as a list

>>> list(df.loc[0]["A"])
[858, 878, 999]

Getting each item

>>> list(df.loc[0]["A"])[0]
858
>>> list(df.loc[0]["A"])[1]
878
>>> list(df.loc[0]["A"])[2]
999
Sign up to request clarification or add additional context in comments.

1 Comment

suppose in my computer the set is { 878, 858, 999} so the result will be 878, 858,999 instead 858, 878, 999.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.