Python Pandas ValueError DataFrame constructor not properly called

0 votes

I am trying to create a Python Pandas Dataframe using this code:

df1=pd.DataFrame('Email')

But I am getting this error:

ValueError: DataFrame constructor not properly called!
Mar 28, 2019 in Python by Shri
85,660 views

3 answers to this question.

+1 vote

The syntax is not right. The right syntax is as follows:

Df1=pd.DataFrame(columns=[‘Email’])
answered Mar 28, 2019 by Ritu
0 votes

You are providing a string representation of a dict to the DataFrame constructor, and not a dict itself. So this is the reason you get that error.

So if you want to use your code, you could do:

df = DataFrame(eval(data))

But better would be to not create the string in the first place, but directly putting it in a dict. Something roughly like:

data = []
for row in result_set:
    data.append({'value': row["tag_expression"], 'key': row["tag_name"]})

But probably even this is not needed, as depending on what is exactly in your result_set you could probably:

  • provide this directly to a DataFrame: DataFrame(result_set)
  • or use the pandas read_sql_query function to do this for you.

Hope it works!!

If you are a beginner and need to know more about Python, It's recommended to go for Python Certification course today.

Thanks!

answered Dec 11, 2020 by Gitika
• 65,770 points
0 votes

It seems you need list:

df2 = pd.DataFrame(['Test'])
print (df2)
      0
0  Test

EDIT:

It seems you need:

def multiple_dfs(file_name, sheet, *args):
    """
    Put multiple dataframes into one xlsx sheet
    """

    row=2
    writer = pd.ExcelWriter(file_name)

    df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'], 
               'income': [40000, 50000, 42000]})
    df2 = pd.DataFrame({'amount': [100, 500, 1000], 
               'income': [40000, 50000, 42000]})
    df3 = pd.DataFrame(['Title'])

    df.to_excel(writer, sheet, startrow=row, index=False)
    row += len(df) + 1
    df2.to_excel(writer, sheet, startrow=row, index=False)
    df3.to_excel(writer, sheet, startrow=0, startcol=0, header=None, \
             index=False)

multiple_dfs('file.xlsx','Sheet1')
answered Dec 11, 2020 by Rajiv
• 8,870 points

Related Questions In Python

–1 vote
1 answer
0 votes
2 answers

Python Pandas error: AttributeError: 'DataFrame' object has no attribute 'rows'

Try this: data=pd.read_csv('/your file name', delim_whitespace=Tru ...READ MORE

answered Dec 10, 2020 in Python by anonymous
• 82,840 points
132,568 views
0 votes
3 answers
0 votes
1 answer

How to create Pandas dataframe from Python list?

You can do it like this: import ...READ MORE

answered Apr 6, 2019 in Python by Likhita
1,286 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,067 views
0 votes
1 answer

DataFrame constructor not properly called! error

You are providing a string representation of ...READ MORE

answered Feb 22, 2022 in Python by Aditya
• 7,680 points
2,465 views
0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,560 points
34,451 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP