如何在Pyspark中取一列的字符串类型小数,然后将其围成最接近的50个值?

发布于 2025-01-29 12:36:11 字数 543 浏览 3 评论 0原文

我在数据集中有一个列,称为“ x”:

x
“ 2893.324”
“ 1058.112”
“ 5651.324”

im试图使这些数字是互插值,该数字是互插的值,圆形到最接近的50个

输出应为:

x结果
“ 2893.324”2900“ 2900
” 1058.112“ 1058.112” 1058.112“1050
“ 5651.324”5650

I have a column in a dataset called "X":

X
"2893.324"
"1058.112"
"5651.324"

Im trying to make these numbers be interger values that round to nearest 50

Output should be this:

Xresults
"2893.324"2900
"1058.112"1050
"5651.324"5650

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2025-02-05 12:36:11

我建议除以50,舍入到最近的整数,然后再次繁殖。

无需用户定义的函数,pyspark.sql.functions模块已覆盖。请参阅建议的代码:

from pyspark.sql.functions import col, round 

df \
.withColumn(
  "results",
  round(col("X").cast("double") / 50) * 50
)

I would suggest dividing by 50, rounding to nearest integer and then multiplying again.

no need for user-defined-functions, pyspark.sql.functions module has you covered. see suggested code:

from pyspark.sql.functions import col, round 

df \
.withColumn(
  "results",
  round(col("X").cast("double") / 50) * 50
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文