• Register
0 votes
26 views

Problem:

I was trying to put some data of my workers in a data frame. I was trying to add their data in an existing Data frame. So I wrote this code:
import pandas as panda 
  

data = 'Name': ['Sam', 'Prince', 'Goragno', 'Abraham'], 
        'Qualification': ['Msc', 'Bsc', 'Msc', 'M.Phil'] 
  

df = panda.DataFrame(data) 
  

address = ['Vancuver', 'New Orland', 'Olulu', 'Gorgia'] 
  

df['Address'] = address 
  

df 

 

But when I run this program it shows:

  File "workers.py", line 4
    data = 'Name': ['Sam', 'Prince', 'Goragno', 'Abraham'], 
                 ^
SyntaxError: invalid syntax

I am newbee please help me to solve this and please explain to me why am I getting this type of errors?

10 7 2
6,060 points

Please log in or register to answer this question.

2 Answers

1 vote

Answer:

The main reason you are getting this error because you did not put your data in a list so you should use 

{}

and open and a closing bracket for this.

And write your code as:

import pandas as panda 
  

data = {'Name': ['Sam', 'Prince', 'Goragno', 'Abraham'], 
        'Qualification': ['Msc', 'Bsc', 'Msc', 'M.Phil']} 
  

df = panda.DataFrame(data) 
  

address = ['Vancuver', 'New Orland', 'Olulu', 'Gorgia'] 
  

df['Address'] = address 
  

df 

Hope this may help you. Happy Coding.

11 6 4
34,950 points
1 vote

Solution:

Pro Tip: When you are getting an error or warning from the Python interpreter you should read the message carefully. The compiler or interpreter always gives a hint of our mistakes. Anyway, in your case, you are getting a SyntaxError because you’ve defined a dictionary with some mixed data. In python, a dictionary starts with a curly brace and ends with the same. For example:

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

Now, have a close look at your defined dictionary. You missed the curly braces. You should have written it this way:

data = {'Name': ['Sam', 'Prince', 'Goragno', 'Abraham'], 'Qualification': ['Msc', 'Bsc', 'Msc', 'M.Phil']}

The dictionary looks good now. Should solve the error. Good day!

13 9 6
94,260 points

Related questions

0 votes
1 answer 36 views
36 views
Problem: I have the following indexed DataFrame with named columns and strings that are not contiguous numbers: a b c d 2 0.671399 0.101208 -0.181532 0.241273 3 0.446172 -0.243316 0.051767 1.577318 5 0.614758 0.075793 -0.451460 -0.012493 I would like to ... different versions join, append, mergebut I did not get the desired result, most only errors. How do I add a column eto the above example?
asked Dec 23, 2020 sasha 16.2k points
0 votes
1 answer 2 views
2 views
Problem: Please let me know how to fix the issue>how to remove a column from a dataframe in python?
asked Apr 11 ashik 14.6k points
0 votes
1 answer 4 views
4 views
Problem: Adding rows to a dataframe in r.
asked Apr 10 Dan phillip 4.8k points
0 votes
1 answer 4 views
4 views
Problem: As I mentioned in my previous query, I'm having difficulties with my learning; I've tried searching the web but haven't been able to find adequate information; could you assist me in continuing my project?
asked Mar 29 tuhin1 48.9k points
0 votes
1 answer 30 views
30 views
Problem: I have large json data that reads into a python dataframe and creates a list of dicts for each line. I need to convert it to a different data format. The data format is as follows: { "data": [{ "item": [{ "value": 0, "type": "a" }, { " ... ) df = df_formatted.fillna(0) The number of items in a list is often in the thousands. Are there pointers or examples on how to do this efficiently?
asked Dec 23, 2020 sasha 16.2k points
0 votes
1 answer 3 views
3 views
Problem: Does anyone know how to remove an entire column from a data.frame in R?
asked Mar 30 ummesalma 29.2k points
0 votes
1 answer 1.2K views
1.2K views
Here's what I've got. I have two data frames. One is a set of financial data that already exists in the system and another set that has some that may or may not exist in the system. I need to find the difference and add the ... pandas\core\frame.py", line 3571, in _compare_frame raise ValueError('Can only compare identically-labeled ' ValueError: Can only compare identically-labeled DataFrame
asked Oct 28, 2020 psandprop 2.4k points
0 votes
1 answer 3 views
3 views
Problem: I have the data.frame. I want to add a column 'g' that classifies my data according to consecutive sequences in column h_no. That is, the first sequence of h_no 1, 2, 3, 4 is group 1, the second series of h_no (1 to 7) is group 2, and so on, as indicated in the last column 'g'.
asked Apr 19 ummesalma 29.2k points
0 votes
1 answer 12 views
12 views
PROBLEM how to add a column to pandas dataframe
asked Jan 24 waji 1.9k points
0 votes
1 answer 5 views
5 views
Problem: I have a data frame and I want to create a new column based on existing columns. Any suggestions?
asked Mar 30 ummesalma 29.2k points