1

I have a .csv file structured as well:

    chan_id   spacecraft                           anomaly_sequences
      P-1       SMAP                 [[2149, 2349], [4536, 4844], [3539, 3779]]

I need to convert the list of list inside the cell anomaly_sequences to a variable, so I can loop inside it. I did it so:

list_labels = df['anomaly_sequences'].values.tolist()

but it converted all the cell in a single list, in fact if I try to print it I see it as:

['[[2149, 2349], [4536, 4844], [3539, 3779]]']

How can I correct it?

9
  • what output do you expect? Commented Oct 2, 2021 at 11:24
  • Can you also provide the output of print(df.dtypes)? Commented Oct 2, 2021 at 11:25
  • @TomerS a list where its first element is [2149, 2349], its second element is [4536, 4844] etc.. Commented Oct 2, 2021 at 11:25
  • @Cimbali chan_id object spacecraft object anomaly_sequences object dtype: object Commented Oct 2, 2021 at 11:26
  • Hmmm I asked the wrong question, that doesn’t tell me if they’re strings or lists… If the objects are lists, df['anomaly_sequences'].explode() should work, if they are strings look at this recent question: stackoverflow.com/questions/69397259 , then do .explode() Commented Oct 2, 2021 at 11:27

1 Answer 1

1

Not sure how you want to present your output, but if you want to convert your string into list of integers list, just do:

import json
var = "[[2149, 2349], [4536, 4844], [3539, 3779]]"
var = json.loads(var)
var

[[2149, 2349], [4536, 4844], [3539, 3779]]

And if you want to flatten the list of list, just use:

flat_list = [item for sublist in var for item in sublist]
flat_list

[2149, 2349, 4536, 4844, 3539, 3779]
Sign up to request clarification or add additional context in comments.

Comments

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.