如何修剪列中的字符串和字符串列表

2024-10-02 04:22:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个df,看起来像这样:

0              ['AU06_threshold_h', 'AU12_threshold_h']  
1                                     AU14_threshold_h   
2                                     AU26_threshold_h   
3                                                  NaN   
4                                     AU01_threshold_h   

我想从每个字符串中删除文本,如果第一个字符是0,则删除零。应该如下所示:

0              [6, 12]  
1                   14 
2                   26   
3                  NaN   
4                    1   

请告知。谢谢你


Tags: 字符串文本dfthresholdnan字符au01au26
2条回答

使用自定义函数(基于regex替换):

In [98]: pat = re.compile(r'[^\d]+')                                                                        

In [99]: def trim_non_num(s): 
    ...:     if isinstance(s, str): 
    ...:         return int(pat.sub('', s)) 
    ...:     elif isinstance(s, list): 
    ...:         return [int(pat.sub('', i)) for i in s] 
    ...:     return s 
    ...:                                                                                                    

In [100]: df['col'].apply(trim_non_num)                                                                     
Out[100]: 
0    [6, 12]
1         14
2         26
3        NaN
4          1
Name: col, dtype: object

使用explode

df.col.explode().str.extract('(\d+)')[0]\
      .groupby(level=0).agg(lambda s: list(s) if len(s)>1 else s.iat[0])

0    [06, 12]
1          14
2          26
3         NaN
4          01
Name: 0, dtype: object

我只能说这不是一个好的设计。避免将列表和数字放在同一列中

相关问题 更多 >

    热门问题