【问题标题】:assign a new col to a dataframe将新列分配给数据框
【发布时间】:2018-11-19 16:04:45
【问题描述】:

我有一个函数,它获取一个数据框并根据表中的每个销售计算一个值并返回一个列表。 我使用以下代码使用函数的输出在表中分配了一个新列:

df.assign(Points = pd.Series(calculate_point(df), index=df.index))

不幸的是,它不起作用,我不知道会出现什么问题。你能帮我弄清楚吗?

谢谢 哈尼

【问题讨论】:

标签: python pandas


【解决方案1】:

只要做:

df['Points'] = pd.Series(calculate_point(df), index=df.index)

让我知道这是否有效。

【讨论】:

    【解决方案2】:

    我所做的是分配这样的列:

    df['newColumnName'] = pd.Series(calculate_point(df), index=df.index)
    

    如果您想要效率,请使用其他:

    df.loc[:,'newColumnName'] = pd.Series(calculate_point(df), index=df.index)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-12
      • 2021-07-05
      • 2021-12-04
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 2020-12-30
      • 2020-09-30
      相关资源
      最近更新 更多