【问题标题】:adding a new column to pandas data frame and fill it with 2 values till the end of the column向 pandas 数据框添加一个新列,并用 2 个值填充它直到该列的末尾
【发布时间】:2017-03-12 05:12:31
【问题描述】:

我想用简单的语法在 pandas 中实现这个简单的 R 代码

这里是R代码

> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
> mtcars$year <- c(1973, 1974)
> head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb year
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 1973
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 1974
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 1973
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 1974
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 1973
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 1974

如您所见,列 year 已添加到数据框中并填充了两个重复值,直到列结束

如何用简单的代码在 pandas 中实现这一点

请注意,我不想在解决方案中使用 for 循环,因为如果我正在处理大数据集,这将花费很多时间。

谢谢!

【问题讨论】:

    标签: python r pandas


    【解决方案1】:

    向 Pandas DF 添加列时,您必须提供长度与 DF 中的行数匹配的对象(除非每个值都相同,在这种情况下为 scalar value can be assigned to the column)。为此,您可以使用生成器表达式重复列表中的元素长度超过 DF 的长度,然后将其切片为正确的长度:

    mtcars['year'] = ([1973, 1974] * (len(mtcars) // 2 + 1))[:len(mtcars)]
    

    感谢 MaxU 提供此解决方案的灵感。

    对于 DF 具有偶数行的情况,您可以简单地将列表的元素重复到 DF 的长度:

    mtcars['year'] = [1973, 1974] * (len(mtcars) // 2) 
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    • 如果您稍微修改一下,您仍然可以将您的解决方案用于具有任意行数的 DF:([1973, 1974] * (len(mtcars) // 2 + 1))[:len(mtcars)]
    • 真的很好!我会把这个添加到答案中
    • 这是一个很棒的调整@MaxU
    【解决方案2】:

    使用 numpy tile(比列表生成技术快得多):

    import numpy as np
    
    years = (1973, 1974)
    mtcars['year'] = np.tile(years, int(len(mtcars) / len(years)) + 1)[:len(mtcars)]
    

    具有 100 万行数据框的 Numpy tile:

    mtcars = pd.DataFrame(np.arange(1000000))
    
    years = (1973, 1974)
    mtcars['year'] = np.tile(years, int(len(mtcars) / len(years)) + 1)[:len(mtcars)]
    
    CPU times: user 0 ns, sys: 4 ms, total: 4 ms
    Wall time: 3.81 ms
    

    使用 100 万行数据框生成列表:

    mtcars['year'] = ([1973, 1974] * (len(mtcars) // 2 + 1))[:len(mtcars)]
    
    CPU times: user 140 ms, sys: 0 ns, total: 140 ms
    Wall time: 136 ms
    

    【讨论】:

      【解决方案3】:

      我建议这样做:

      def new_vect(vect, n_row):
          l_vect = len(vect)
          l_new_vect = n_row / l_vect + 1
          new_vect = vect * l_new_vect
          return new_vect[:n_row]
      
      mtcars['year'] = new_vect([1973,1974],mtcars.shape[0])
      

      这可能有点复杂,但它也适用于偶数行

      【讨论】:

        猜你喜欢
        • 2017-10-16
        • 1970-01-01
        • 2023-02-03
        • 2021-08-09
        • 2020-11-26
        • 1970-01-01
        • 2019-11-14
        • 2017-02-10
        • 1970-01-01
        相关资源
        最近更新 更多