我有如下数据。其中包含产品ID,制造成本,税1,税2和MRP。 因此,我必须创建一个新列来确定该项目的售价。 售价显示符合以下条件
例如,MC + TX1 = 70,MC + TX2 = 80,MRP = 100,然后售价= 80
如果MC + TX1 = 170,MC + TX2 = 80,MRP = 100,则销售价格= 80,因为MC + tx1或MX + tx2中的一个小于MRP,那么我们将采用该值
MC + TX1 = 170,MC + TX2 = 180,MRP = 100,则售价= 100
TFIDF = TfidfVectorizer(tokenizer=tokenizerFunc,analyzer= 'word',min_df=3,token_pattern=r'(?u)\b[A-Za-z]+\b',stop_words= 'english')
tfidf_matrix = TFIDF.fit_transform(df2['job_title'])
所需的输出
+------------+-------------+-------+-------+-----+--+
| Product ID | Making Cost | Tax 1 | Tax 2 | MRP | |
+------------+-------------+-------+-------+-----+--+
| 12345 | 50 | 20 | 30 | 100 | |
| 23456 | 50 | 60 | 30 | 100 | |
| 34567 | 50 | 60 | 70 | 100 | |
+------------+-------------+-------+-------+-----+--+
答案 0 :(得分:1)
您可以在以下所有条件下使用
Master = new SearchPage(true)
{
Title = "Search Page"
};
输出:
val df = spark.sparkContext.parallelize(Seq(
(12345, 50, 20, 30, 100),
(23456, 50, 60, 30, 100),
(34567, 50, 60, 70, 100)
))
.toDF("ID", "MC", "T1", "T2", "MRP")
df.withColumn("SP",
when((($"MC" + $"T1").gt($"MRP")) && (($"MC" + $"T2").gt($"MRP")), $"MRP")
.otherwise(
when((($"MC" + $"T1").lt($"MRP")) && (($"MC" + $"T2").lt($"MRP")), greatest(($"MC" + $"T1"), ($"MC" + $"T2")))
.otherwise(
when((($"MC" + $"T1").gt($"MRP")), $"MC" + $"T2")
.otherwise($"MC" + $"T1")
)
)
).show(false)
要提高效率,您可以先计算所有+-----+---+---+---+---+---+
|ID |MC |T1 |T2 |MRP|SP |
+-----+---+---+---+---+---+
|12345|50 |20 |30 |100|80 |
|23456|50 |60 |30 |100|80 |
|34567|50 |60 |70 |100|100|
+-----+---+---+---+---+---+
和MC+T1
,这样就不必每次都计算,不需要时也可以计算。
答案 1 :(得分:0)
您可以使用spark sql或dataframe API来实现
df.withColumn("selling_price",
when((col("MC") + col("TX1") < col("MC") + col("TX2")) && (col("MC") + col("TX2") < col("MRP") , col("MC") + col("TX2"))
.when((col("MC") + col("TX1") > col("MRP")) && (col("MC") + col("TX2") > col("MRP") , col("MRP"))
.otherwise(col("MC") + col("TX1")))