【问题标题】:Append based on length of string根据字符串长度追加
【发布时间】:2016-07-08 20:00:03
【问题描述】:

我有一个像这样的df:

Year   Month  Day
1984   1      1
1985   12     22

我想让MonthDay 无论如何都有两位数。所以我想要的数据框是这样的:

Year   Month  Day
1984   01     01
1985   12     22

我一直在玩这个:

for i in df.Month:
    i=str(i)
    if len(i) < 2:
        i='0' + i
    print i

但我不确定如何将新值实际重新插入数据框中,我很确定首先有更好的方法来做到这一点

【问题讨论】:

    标签: python pandas python-2.x


    【解决方案1】:

    您应该让那些DataFrames 保持原样,并且只有在您需要生成报告时才应该担心这个演示问题。

    然后就变成了一般的字符串格式化问题(see also format())。下面显示的是 (1) 转换为字符串,(2) 填充到长度的两个带前导空格,(3) 填充到长度的两个带前导零:

    >>> ['{}'.format(x) for x in range(10)]
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    >>> ['{:2}'.format(x) for x in range(10)]
    [' 0', ' 1', ' 2', ' 3', ' 4', ' 5', ' 6', ' 7', ' 8', ' 9']
    >>> ['{:02}'.format(x) for x in range(10)]
    ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']
    

    【讨论】:

      【解决方案2】:

      我认为使用to_datetime 创建日期列以使用numpy datetime 数据类型可能是个好主意。看起来这会给您提供接近您想要的格式,但是您也可以使用dt.strftime 使用您想要的任何格式从这里格式化您的日期:

      df['Date'] = pd.to_datetime(df.Year.astype(str) + ' ' 
                                + df.Month.astype(str) + ' ' 
                                + df.Day.astype(str))
      
      df['Date']
      
      0   1984-01-01
      1   1985-12-22
      Name: Date, dtype: datetime64[ns]
      
      df.Date.dt.strftime("%Y %m %d")
      
      0    1984 01 01
      1    1985 12 22
      

      【讨论】:

        【解决方案3】:

        请试试这个选项。如果您希望月份和日期各有 2 位数字。

        for i in df.Month:
            i=str(i)
            print('%02d'%(i,)) #this is for python 3.4.4 in previous version this may be print "%02d" % (i,)
        

        【讨论】:

          【解决方案4】:

          您可以使用astype 转换为stringzfill 填充0

          #df['Year'] = df['Year'].astype(str) #if column Year has to be string
          df['Month'] = df['Month'].astype(str).str.zfill(2)
          df['Day'] = df['Day'].astype(str).str.zfill(2)
          print df
             Year Month Day
          0  1984    01  01
          1  1985    12  22
          

          如果所有列中的type 必须转换为string

          df = df.astype(str) 
          df['Month'] = df['Month'].str.zfill(2)
          df['Day'] = df['Day'].str.zfill(2)
          print df
          

          时间安排

          In [225]: %timeit df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
          1 loops, best of 3: 500 ms per loop
          
          In [226]: %timeit a(df)
          100 loops, best of 3: 10.8 ms per loop
          

          代码

          df1 = df.copy()
          
          def a(df):
              df = df.astype(str); 
              df['Month'] = df['Month'].str.zfill(2);
              df['Day'] = df['Day'].str.zfill(2);
              return df
          
          print df1.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
          print a(df)
          

          【讨论】:

          • 或使用 oneliner:df.apply(lambda x: x.astype(str).str.zfill(2), axis=1)
          • 是的,它很好,但速度很慢。我会添加一些时间。
          • 很有趣,它慢了50倍。
          • 它为什么这么慢很有趣。我认为他们应该有几乎相同的时间......
          • 我认为没有applyzfill 是矢量化的。它可以解释更好的性能。
          猜你喜欢
          • 2012-01-19
          • 1970-01-01
          • 1970-01-01
          • 2013-11-25
          • 2017-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多