【问题标题】:Removing leading zeros from pandas.core.series.Series从 pandas.core.series.Series 中删除前导零
【发布时间】:2018-06-16 18:45:05
【问题描述】:

我有一个带有数据的 pandas.core.series.Series

0    [00115840, 00110005, 001000033, 00116000...
1    [00267285, 00263627, 00267010, 0026513...
2                             [00335595, 00350750]

我想从系列中删除前导零。我试过了

x.astype('int64')

但收到错误消息

ValueError: setting an array element with a sequence.

你能建议我如何在 python 3.x 中做到这一点吗?

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:
    #where x is a series
    x = x.str.lstrip('0')  
    

    【讨论】:

      【解决方案2】:

      如果您想要更清晰的解决方案,可以尝试以下操作: 假设 a 是原始系列。

      b = a.explode().astype(int)
      a = b.groupby(b.index).agg(list)
      

      尽管这比 @cs95 和 @jezrael 发布的解决方案要慢

      【讨论】:

        【解决方案3】:

        如果您有混合 dtype,下面的行应该可以工作

        df['col'] = df['col'].apply(lambda x:x.lstrip('0') if type(x) == str else x)

        【讨论】:

          【解决方案4】:

          如果要将strings 列表转换为integerss 列表,请使用list comprehension

          s = pd.Series([[int(y) for y in x] for x in s], index=s.index)
          
          s = s.apply(lambda x: [int(y) for y in x])
          

          示例:

          a = [['00115840', '00110005', '001000033', '00116000'],
               ['00267285', '00263627', '00267010', '0026513'],
               ['00335595', '00350750']]
          
          s = pd.Series(a)
          print (s)
          0    [00115840, 00110005, 001000033, 00116000]
          1      [00267285, 00263627, 00267010, 0026513]
          2                         [00335595, 00350750]
          dtype: object
          
          s = s.apply(lambda x: [int(y) for y in x])
          print (s)
          0    [115840, 110005, 1000033, 116000]
          1      [267285, 263627, 267010, 26513]
          2                     [335595, 350750]
          dtype: object
          

          编辑:

          如果只需要integers,您可以将值展平并转换为ints:

          s = pd.Series([item for sublist in s for item in sublist]).astype(int)
          

          替代解决方案:

          import itertools
          s = pd.Series(list(itertools.chain(*s))).astype(int)
          
          print (s)
          0     115840
          1     110005
          2    1000033
          3     116000
          4     267285
          5     263627
          6     267010
          7      26513
          8     335595
          9     350750
          dtype: int32
          

          时间安排

          a = [['00115840', '00110005', '001000033', '00116000'],
               ['00267285', '00263627', '00267010', '0026513'],
               ['00335595', '00350750']]
          
          s = pd.Series(a)
          s = pd.concat([s]*1000).reset_index(drop=True)
          
          In [203]: %timeit pd.Series([[int(y) for y in x] for x in s], index=s.index)
          100 loops, best of 3: 4.66 ms per loop
          
          In [204]: %timeit s.apply(lambda x: [int(y) for y in x])
          100 loops, best of 3: 5.13 ms per loop
          
          #cᴏʟᴅsᴘᴇᴇᴅ sol
          In [205]: %%timeit
               ...: v = pd.Series(np.concatenate(s.values.tolist()))
               ...: v.astype(int).groupby(s.index.repeat(s.str.len())).agg(pd.Series.tolist)
               ...: 
          1 loop, best of 3: 226 ms per loop
          
          #Wen solution
          In [211]: %timeit pd.Series(s.apply(pd.Series).stack().astype(int).groupby(level=0).apply(list))
          1 loop, best of 3: 1.12 s per loop
          

          扁平化解决方案(@cᴏʟᴅsᴘᴇᴇᴅ 的想法):

          In [208]: %timeit pd.Series([item for sublist in s for item in sublist]).astype(int)
          100 loops, best of 3: 2.55 ms per loop
          
          In [209]: %timeit pd.Series(list(itertools.chain(*s))).astype(int)
          100 loops, best of 3: 2.2 ms per loop
          
          #cᴏʟᴅsᴘᴇᴇᴅ sol
          In [210]: %timeit pd.Series(np.concatenate(s.values.tolist()))
          100 loops, best of 3: 7.71 ms per loop
          

          【讨论】:

          • 什么是x @jezrael
          • @pyd - 它是 lambda 或列表理解变量。
          【解决方案5】:
          s=pd.Series(s.apply(pd.Series).astype(int).values.tolist())
          s
          Out[282]: 
          0    [1, 2]
          1    [3, 4]
          dtype: object
          

          数据输入

          s=pd.Series([['001','002'],['003','004']])
          

          更新:感谢 Jez 并冷冷地指出 :-)

          pd.Series(s.apply(pd.Series).stack().astype(int).groupby(level=0).apply(list))
          Out[317]: 
          0    [115840, 110005, 1000033, 116000]
          1      [267285, 263627, 267010, 26513]
          2                     [335595, 350750]
          dtype: object
          

          【讨论】:

          • 我认为如果列表大小不同,您的解决方案将失败,请检查我的示例数据。 :(
          • @cᴏʟᴅsᴘᴇᴇᴅ 已修复:-)
          • @Wen - 添加到计时中。
          【解决方案6】:

          使用np.concatenate 扁平化您的数据 -

          s
          
          0    [00115840, 36869, 262171, 39936]
          1     [00267285, 92055, 93704, 11595]
          2                  [00335595, 119272]
          Name: 1, dtype: object
          
          v = pd.Series(np.concatenate(s.tolist()))
          

          或者(感谢 jezrael 的建议),使用更快的 .values.tolist -

          v = pd.Series(np.concatenate(s.values.tolist()))
          

          v
          
          0    00115840
          1       36869
          2      262171
          3       39936
          4    00267285
          5       92055
          6       93704
          7       11595
          8    00335595
          9      119272
          dtype: object
          

          现在,你对 astype 所做的应该可以工作了 -

          v.astype(int)
          
          0    115840
          1     36869
          2    262171
          3     39936
          4    267285
          5     92055
          6     93704
          7     11595
          8    335595
          9    119272
          dtype: int64
          

          如果您有浮点数据,请改用astype(float)


          如果您愿意,可以使用 groupby + agg 将结果重新调整为原始格式 -

          v.astype(int).groupby(s.index.repeat(s.str.len())).agg(pd.Series.tolist)
          
          0    [115840, 36869, 262171, 39936]
          1     [267285, 92055, 93704, 11595]
          2                  [335595, 119272]
          dtype: object
          

          【讨论】:

          • pd.Series(np.concatenate(s.values.tolist())) ?
          • @jezrael s.tolist() 有效,还是我错过了什么? :-)
          • 它更快,;)
          • @jezrael 太棒了!感谢您的建议。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-04-09
          • 2017-06-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多