简体   繁体   English

Pandas Python中的拆分列表

[英]Splitting List in Pandas Python

I have a list which output like this我有一个列表,其中 output 像这样

[                   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 所以列表 output 应该是

[[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我在这里做了 append 因为每次迭代都会在 dfo 中创建一个新列表以将其添加到我使用 append 的列表中,然后我尝试将其拆分为 0

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

It says this error Dataframe has no variable split它说这个错误 Dataframe has no variable split

You can use the split operation for strings , but not for dataframes .您可以对strings使用split操作,但不能对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 ):假设您的原始列表包含string元素,您可以尝试直接在此列表上使用split (无需转换为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).每次满足条件时(第一次除外),这都会将 append 一个新的子列表添加到 output 列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM