• Register
0 votes
22 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,240 points

Related questions

0 votes
1 answer 13 views
13 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 5.3k points
0 votes
1 answer 7 views
7 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 5.3k points
0 votes
1 answer 20 views
20 views
Problems I am trying to update selected datetime64 values in a pandas data frame using the loc method to select rows satisfying a condition. However, instead of assigning the new date-time value it results in NaT. Here is a simplification of my code that shows the problem: ... as the second element in the new_date column. Any ideas on how this should be done or why this is not working as intended?
asked Sep 15, 2020 Marivoke 530 points
0 votes
1 answer 10 views
10 views
Problem: I am having a DataFrame using pandas and column labels that I want to edit to replace the correct column labels. I would like to replace the column names in the DataFrame A where the original column names are below: ['$a', '$b', '$c', '$d', '$e'] TO ['a', 'b', 'c', 'd', 'e']. I have corrected column names that are saved in a list, please help me with it.
asked Dec 23, 2020 sasha 5.3k points
0 votes
1 answer 104 views
104 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 27 views
27 views asked Oct 30, 2020 john 580 points
0 votes
1 answer 1.6K views
1.6K views
I am trying to create a rating data frame for some football teams. To rate the teams I am using elo rating system : https://en.wikipedia.org/wiki/Elo_rating_system My data frame consists of pre-match ratings of home and away teams and it gets updated after every match. My data ... of size 1 to a python scalar Can any one help me to find what the problem really is and help me to solve the problem.
asked Mar 10, 2020 LizzyM 6.1k points
0 votes
0 answers 21 views
21 views
I have a dataframe of almost 120000 records as follows. Also I have a mongoDB collection which looks exacly same as below dataframe ItemID ParentID ItemRating ItemPrice Qty A1 ItemA1 0 12 100 A2 ItemA2 0 15 200 B1 ItemB1 0 20 300 B2 ItemB2 0 25 400 B3 ItemB3 0 30 ... PyMongo update_many method by setting upsert=true. but I am not sure how can I do that ? how should I write my filter condition ?
asked Sep 13, 2020 NguyenTram 1k points
0 votes
1 answer 1K views
1K views
Problem: I have only fundamental knowledge related to python, pandas and dataframe.I have tried to write the below code: df = pd.DataFrame(np.random.rand(12,2), columns=['Apples', 'Oranges'] ) df['Categories'] = pd.Series(list('AAAABBBBCCCC')) pd.options.display. ... I will be more than glad to get more ideas on the above error regarding why it is occurring and also how to fix this error?
asked Aug 24, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 11 views
11 views
Problem: Hello all, I’ve just completed learning basic python. You may treat me as an intermediate-level python developer though I know learning has no limit in the programming field. Recently, I started learning data analysis using python pandas. Therefore, I am ... and share me the procdure to achieve this? Thanks a lot for reading my problem attentively and your reply to this thread.
asked Jul 5, 2020 adamSw 11.3k points