将熊猫列中的字符串拆分为多个字符串

时间:2021-06-20 23:45:22

标签: python pandas string

如果这个问题已经得到回答,我很抱歉,但我没有找到任何答案。我想在多个字符串中拆分和转换长字符串 我有数据框 df:

       no         strings
1.  A_12_234   gef|re1234|gef|re0943
2.  O_257363   tef|fe4545|tef|fe3333|tef|9995

我想制作单个字符串并创建新列

我得到的输出:

       no         strings                          new_col
1.  A_12_234   gef|re1234|gef|re0943                <thekeys db="gef" value="re1234"/>\n<thekeys db="gef" value="re0943"/>

2.  O_257363   tef|fe4545|tef|fe3333|tef|9995       <thekeys db="tef" value="fe4545"/>\n<thekeys db="tef" value="fe3333"/>

所需的输出:

         no         strings                          new_col
1.  A_12_234   gef|re1234|gef|re0943                <thekeys db="gef" value="re1234"/>\n<thekeys db="gef" value="re0943"/>

2.  O_257363   tef|fe4545|tef|fe3333|tef|9995       <thekeys db="tef" value="fe4545"/>\n<thekeys db="tef" value="fe3333"/>\n<thekeys db="tef" value="9995"/>

我不知道我哪里出错了,因为它跳过了一些对

代码如下:

def createxm(x):
try:
    parsedlist = x['strings'].split('|')
    print(parsedlist)
    cnt = len(parsedlist)/2
    print(cnt)
    xm_list = []
    for i in range(0, int(cnt), 2):
        xm_list.append('<thekeys db="{}" value="{}"/>'.format(parsedlist[i], parsedlist[i+1]))
        xm_string = '\n'.join(xml_list)
    return xm_string
except:
    return None

谢谢

1 个答案:

答案 0 :(得分:1)

你快到了。问题出在分割 using (ClientContext spClientContext = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, _clientId, _clientSecret)) { if (spClientContext != null) { CamlQuery camlQuery = new CamlQuery(); camlQuery.FolderServerRelativeUrl = getSiteUrlAbsolutePath(siteUrl) + "/Shared Documents/" + folderPath; camlQuery.ViewXml = "<View Scope="RecursiveAll">" + "<Query>" + "<Where>" + "<Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq>" + "</Where>" + "<OrderBy><FieldRef Name=\"item.File.Name\" Ascending=\"FALSE\"/></OrderBy>" + "</Query>" + "</View>"; List list = spClientContext.Web.Lists.GetByTitle("Documents"); ListItemCollection listItems = list.GetItems(camlQuery); spClientContext.Load(listItems, items => items.Include( item => item.DisplayName, item => item.FileSystemObjectType, item => item.File, item => item.File.Name, item => item.File.Author, item => item.File.ModifiedBy, item => item.File.ListItemAllFields["Created"], item => item.File.ListItemAllFields["Modified"], item => item.File.ListItemAllFields["FileRef"], item => item.File.Length)); spClientContext.ExecuteQuery(); if (listItems != null && listItems.Count > 0) { foreach (ListItem item in listItems) { if (item.FileSystemObjectType.Equals(FileSystemObjectType.File)) { // Do Stuff } } } } } 的地方。

更正的代码:

cnt = len(parsedlist/2)

打印:

def createxm(x):
    try:
        parsedlist = x['strings'].split('|')
        print(parsedlist)
        cnt = len(parsedlist)
        print(cnt)
        xm_list = []
        for i in range(0, int(cnt), 2):
            xm_list.append('<thekeys db="{}" value="{}"/>'.format(parsedlist[i], parsedlist[i+1]))
            xm_string = '\n'.join(xm_list)
        return xm_string
    except:
        return None
df['new_col'] = df.apply(lambda x:createxm(x), axis=1)