Darshan Malu
Darshan Malu

Reputation: 63

Splitting List in Pandas Python

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

Upvotes: 0

Views: 58

Answers (1)

Gerd
Gerd

Reputation: 2813

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

Upvotes: 2

Related Questions