python - 如何像我们在 pyspark withColumn 中那样在 pandas 中动态创建新列

标签 python pandas pyspark

from statistics import mean
import pandas as pd
df = pd.DataFrame(columns=['A', 'B', 'C'])
df["A"] = [1, 2, 3, 4, 4, 5, 6]
df["B"] = ["Feb", "Feb", "Feb", "May", "May", "May", "May"]
df["C"] = [10, 20, 30, 40, 30, 50, 60]
df1 = df.groupby(["A","B"]).agg(mean_err=("C", mean)).reset_index()

df1["threshold"] = df1["A"] * df1["mean_err"]

而不是最后一行代码,我怎么能像在 Pyspark .withColumn() 中那样做呢?

enter image description here

此代码无效。我想像在 Pyspark 的 withColumn 方法中一样,通过动态使用操作输出来创建新列。

有人知道怎么做吗?

最佳答案

选项 1:DataFrame.eval

(df.groupby(['A', 'B'], as_index=False)
   .agg(mean_err=('C', 'mean'))
   .eval('threshold = A * mean_err'))

选项 2:DataFrame.assign

(df.groupby(['A', 'B'], as_index=False)
   .agg(mean_err=('C', 'mean'))
   .assign(threshold=lambda x: x['A'] * x['mean_err']))

   A    B  mean_err  threshold
0  1  Feb      10.0       10.0
1  2  Feb      20.0       40.0
2  3  Feb      30.0       90.0
3  4  May      35.0      140.0
4  5  May      50.0      250.0
5  6  May      60.0      360.0

关于python - 如何像我们在 pyspark withColumn 中那样在 pandas 中动态创建新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72291290/

相关文章:

python - 自动 - 递增 pyspark 数据框列值

python - 如何在 PySpark 中使用窗口函数?

python - 使用 Python C API 时出现 ImportError

python - 使用 print 语句在 Python 中显示变量值?

python - tf.GradientTape() 返回 None

python - 如何使用 pandas melt 获取值及其错误

python - 如何查找 Pyspark 中列中值最大的行名称

python - Python 中的嵌套 While 循环

python - 从文本文件提取数据到 Pandas 时如何忽略垃圾数据?

python - 如何用 pandas 计算百分比和累积百分比