0

I have a list which output like this

[                   0
0                 PV
1               Conf
2                 32
3                 PF
4               Test
5             Output
6           I/O Test,                    0
0             PVER-I
1          PVER-Conf
2                BFT
3             PVER-F
4           COM Test
5  Output State Test]

I want to split it as 0 so list output should be

[[0 PV, 1 Conf,2 32,.....],[0 PVER-1,......5 Output State Test]]

I tried doing this My previous 2 line codes are

    dfo= pd.DataFrame(df_z9[0].str.split().values.tolist())
    list.append(dfo)

I did append here because every iteration a new list was created in dfo to add this to the list i used append and then I tried to split it at 0

for item in list 
    item.split('0', 1)[0]

It says this error Dataframe has no variable split

1 Answer 1

2

You can use the split operation for strings, but not for dataframes. Assuming that your original list contains string elements, you can try to use split directly on this list (without the conversion to a dataframe):

out_list = [[]]
sublist = 0
for item in list:
  if item.split('0')[0] == '' and item is not list[0]:
      out_list.append([])
      sublist += 1
  out_list[sublist].append(item)

This will append a new sublist to the output list every time the condition is met (except for the first time).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Worked perfectly. This saved a lot of time for me.

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.