TypeError: 'DataFrame' object does not support item assignment when using division

0

TypeError: 'DataFrame' object does not support item assignment when using division.

Dividing two integer columns in a pyspark dataframe and receiving this error. Any ideas?

enter image description here

Reproducible:

simpleData = (((1, 2), (2, 6), (3, 6))
columns= ["sales","users"]
df = spark.createDataFrame(data = simpleData, schema = columns)
df = df.withColumn("users", df["users"].cast(IntegerType()))
df = df.withColumn("sales", df["sales"].cast(IntegerType()))
df['buy_rate'] = df['sales']/ df['users']
databricks division pyspark
2021-11-23 19:30:39
1

1

just small tweak .

from pyspark.sql.types import IntegerType
import pyspark.sql.functions as F
simpleData=((1, 2), (2, 6), (3, 6))
columns= ["sales","users"]
df = spark.createDataFrame(data = simpleData, schema = columns)
df = df.withColumn("users", df["users"].cast(IntegerType()))
df = df.withColumn("sales", df["sales"].cast(IntegerType()))

df1 = df\
    .withColumn("buy_rate", (F.col("sales") / F.col("users")))
display(df1)

enter image description here

2021-11-23 20:01:03

Thanks Karthik, and very good approach with the assignment to the new column within the withColumn() this worked beautifully.
Greg

best wishes Greg !!
Karthikeyan Rasipalay Durairaj

In other languages

This page is in other languages

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................