0

I am new to python and I am facing problem in creating a Dataframe using pandas:

import pandas as pd

df = spark.createDataFrame([(66, "a", "4"), 
                            (67, "a", "0"), 
                            (70, "b", "4"), 
                            (71, "d", "4")],
                            ("id", "code", "amt"))


dfa = pd.DataFrame(data=df)

This is the error I am getting ValueError: DataFrame constructor not properly called!

7
  • 2
    Do you have to start with spark?. You can pass that list to the dataframe constructor directly Commented Feb 24, 2021 at 3:39
  • 2
    You also need to be more specific than "i am facing a problem". What kind of problem? Are the data in the wrong order? Is an error raised? Commented Feb 24, 2021 at 3:41
  • 1
    OK, so the dataframe constructor was not properly called. Did you read the docs on how to call it? What wasn't clear about those docs? Commented Feb 24, 2021 at 3:41
  • 2
    But did you look at the official documentation? it has many example of creating dataframes Commented Feb 24, 2021 at 3:46
  • 1
    That's not a pandas dataframe, it's a PySpark one. And please add the missing 'import' for spark Commented Feb 24, 2021 at 3:49

2 Answers 2

1
dfa = df.select("*").toPandas()

see https://spark.apache.org/docs/latest/sql-pyspark-pandas-with-arrow.html#enabling-for-conversion-tofrom-pandas

Sign up to request clarification or add additional context in comments.

Comments

0

What is the error message? Have you imported spark or just pandas? Why are you using spark instead of pandas for creating the dataframe, just go as

pd.DataFrame([(66, "a", "4"), 
              (67, "a", "0"), 
              (70, "b", "4"), 
              (71, "d", "4")], columns=("id", "code", "amt"))

And it will make a dataframe for you.

    id      code    amt
0   66      a       4
1   67      a       0
2   70      b       4
3   71      d       4

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.