【问题标题】:Rounding up one column in pandas dataframe舍入熊猫数据框中的一列
【发布时间】:2017-05-09 06:17:52
【问题描述】:

我有一个熊猫数据框df,看起来像这样:

          no_obs  price_cleaning  house_size
0         1             585          30
1         1             585          40
2         1             585          43
3         1             650          43
4         1             633          44
5         1             650          45
6         2             585          50
7         1             633          50
8         1             650          50
9         2             750          50 

我想用这个函数对price_cleaning 列中的值进行四舍五入:

def roundup(x): return int(math.ceil(x / 10.0)) * 10

我已经尝试了这个答案 (Applying function to Pandas dataframe by column) 的解决方案:

cols = [col for col in df.columns if col != 'price_cleaning'] df[cols] = df[cols].apply(roundup)

我收到以下错误: TypeError: ("cannot convert the series to ", '发生在索引 no_obs')

谁能帮我理解为什么这不起作用?如何将汇总函数应用于列?非常感谢任何帮助。

【问题讨论】:

    标签: python python-3.x pandas python-3.5


    【解决方案1】:

    这可能有效:

    >>> df['price_cleaning_ceiling']= df.price_cleaning.apply(lambda x: int(math.ceil(x / 10.0)) * 10)
    

    【讨论】:

      【解决方案2】:

      我认为您可以将applylambda 用作:

      In [6]: df['p'] = df['price_cleaning'].apply(lambda x: int(math.ceil(x / 10.0)) * 10)
      
      In [7]: df
      Out[7]: 
         no_obs  price_cleaning  house_size    p
      0       1             585          30  590
      1       1             585          40  590
      2       1             585          43  590
      3       1             650          43  650
      4       1             633          44  640
      5       1             650          45  650
      6       2             585          50  590
      7       1             633          50  640
      8       1             650          50  650
      9       2             750          50  750
      

      【讨论】:

        【解决方案3】:

        您正在倒置过滤列,请改为:

        cols = [col for col in  df.columns if col == 'price_cleaning']
        

        现在,如果您只需要清理一列,则无需创建cols。做吧:

        df['price_cleaning'] = df['price_cleaning'].apply(roundup)
        

        【讨论】:

        • apply 方法很棒。但是,根据您的用例、数据大小,您可以使用矢量化方法进行基准测试。
        • @JohnGalt 我认为 OP 在这一点上与您的准确评论相去甚远
        • 没错,这只是为了后代的缘故:)
        【解决方案4】:

        我会像矢量化一样

        In [298]: df['p'] = (np.ceil(df.price_cleaning / 10) * 10).astype(int)
        
        In [299]: df
        Out[299]:
           no_obs  price_cleaning  house_size    p
        0       1             585          30  590
        1       1             585          40  590
        2       1             585          43  590
        3       1             650          43  650
        4       1             633          44  640
        5       1             650          45  650
        6       2             585          50  590
        7       1             633          50  640
        8       1             650          50  650
        9       2             750          50  750
        

        对于 10K 行,时间 - 向量化方法比 apply 快约 15 倍

        In [331]: %timeit (np.ceil(dff.price_cleaning / 10) * 10).astype(int)
        1000 loops, best of 3: 436 µs per loop
        
        In [332]: %timeit dff['price_cleaning'].apply(roundup)
        100 loops, best of 3: 7.86 ms per loop
        
        In [333]: dff.shape
        Out[333]: (10000, 4)
        

        至少在这种情况下,行数越多,性能差距就会越大。

        【讨论】:

        • 使用 np.ceil 的好答案,非常有用,谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-03
        • 2017-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多