Spark DF pivot error: Method pivot([class java.lang.String, class java.lang.String]) does not exist

NITS

I am a newbie at using Spark dataframes. I am trying to use the pivot method with Spark (Spark version 2.x) and running into the following error:

Py4JError: An error occurred while calling o387.pivot. Trace: py4j.Py4JException: Method pivot([class java.lang.String, class java.lang.String]) does not exist

Even though I have the agg function as first here, I really do not need to apply any aggregation.

My dataframe looks like this:

+-----+-----+----------+-----+
| name|value|      date| time|
+-----+-----+----------+-----+
|name1|100.0|2017-12-01|00:00|
|name1|255.5|2017-12-01|00:15|
|name1|333.3|2017-12-01|00:30|

Expected:

+-----+----------+-----+-----+-----+
| name|      date|00:00|00:15|00:30|
+-----+----------+-----+-----+-----+
|name1|2017-12-01|100.0|255.5|333.3|

The way I am trying:

df = df.groupBy(["name","date"]).pivot(pivot_col="time",values="value").agg(first("value")).show

What is my mistake here?

Shaido

The problem is the values="value" parameter in the pivot function. This should be used for a list of actual values to pivot on, not a column name. From the documentation:

values – List of values that will be translated to columns in the output DataFrame.

and an example:

df4.groupBy("year").pivot("course", ["dotNET", "Java"]).sum("earnings").collect()
[Row(year=2012, dotNET=15000, Java=20000), Row(year=2013, dotNET=48000, Java=30000)]

For the example in the question values should be set to ["00:00","00:15", "00:30"]. However, the values argument is often not necessary (but will make the pivot more efficient), so you can simply change to:

df = df.groupBy(["name","date"]).pivot("time").agg(first("value"))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Method showString([class java.lang.Integer, class java.lang.Integer, class java.lang.Boolean]) does not exist in PySpark

java.lang.NoSuchMethodError: No virtual method toString(Z)Ljava/lang/String; in class Lokhttp3/Cookie; or its super classe

Attempt to invoke virtual method 'java.lang.String java.lang.String.toUpperCase()' on a null object reference

Multiple Class Java error: Ljava.lang.String

How to solve this error "cannot resolve method 'addData(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)'"?

Apache spark Row getAs[String] : java.lang.Byte cannot be cast to java.lang.String

java.lang.IllegalArgumentException: 'json' argument must be an instance of: [class java.lang.String, class [B

Cannot cast class java.lang.Integer to class java.lang.String

Error java.lang.AssertionError: expected: null<null> but was: java.lang.String<null> what does it mean?

JMeter - Static method get( java.lang.String ) not found in class'java.nio.file.Paths'

Spark __getnewargs__ error ... Method or([class java.lang.String]) does not exist

Pyspark error - py4j.Py4JException: Method limit([class java.lang.String]) does not exist

java.lang.String is not a valid external type for schema of int error in creating spark dataframe

pyspark py4j.Py4JException: Method and([class java.lang.Integer]) does not exist

SQLite error: java.lang.IllegalArgumentException: column '_id' does not exist

No such property: id for class: java.lang.String

java.lang.String class' value variable

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

Cannot resolve method put(java.lang.string, java.lang.string)

constructor defined to take String but method invocation shows error 'Array type expected found java.lang.String'

org.testng.TestNGException: Cannot inject @Test annotated Method with [class java.lang.String]

Why the method valueOf(String name) of enum is not defined in the abstract class java.lang.Enum?

java.lang.NoSuchMethodError: No virtual method getMccString()Ljava/lang/String;

No such property: split for class: java.lang.String

Exception in thread "main" java.lang.ClassFormatError: Duplicate method name "setName" with signature "(Ljava.lang.String;)V" in class file Parent

addArguments(java.lang.String... arguments) method of ChromeOptions is giving error

class java.lang.Long cannot be cast to class java.lang.String error when having a hashmap of mixed types (Long and string)

Error: EL1004E: Method call: Method rename(java.lang.String,java.lang.String) cannot be found on type com.jcraft.jsch.ChannelSftp$2

Gatling getting 'cannot find symbol [ERROR] symbol: method Cookie(java.lang.String,java.lang.String)' Error

TOP Ranking

HotTag

Archive