Error when pull data from dictionary and path-combine in pandas

Jasch

I tried to pull data from 3 cities. How can I read all 3 city data instead of reading one by one below? Do I have duplicated code for reading data below? How to read data from dictionary to avoid the error? Thanks so much.

import csv
with open('C:\\Users\\jasch\\chicago.csv') as chicago_data:
    csvReader = csv.reader(chicago_data)

import csv
with open('C:\\Users\\jasch\\new_york_city.csv') as new_york_data:
    csvReader = csv.reader(new_york_data)

import csv
with open('C:\\Users\\jasch\\washington.csv') as washington_data:
    csvReader = csv.reader(washington_data)

import time
import pandas as pd
import numpy as np

CITY_DATA = { 'chicago': 'chicago.csv',
              'new york city': 'new_york_city.csv',
              'washington': 'washington.csv' }

df = pd.read_csv(CITY_DATA[city])

df['Start Time'] = pd.to_datetime(df['Start Time'])
df['month'] = df['Start Time'].dt.month
print (df['month'])

NameError                                 Traceback (most recent call last)
<ipython-input-16-b1588646f194> in <module>()
      7               'washington': 'washington.csv' }
      8 
----> 9 df = pd.read_csv(CITY_DATA[city])
     10 
     11 df['Start Time'] = pd.to_datetime(df['Start Time'])

NameError: name 'city' is not defined

3. csv files of city data have almost the same column names below.

     Start Time             End Time  Trip Duration  \
0  2017-05-29 18:36:27  2017-05-29 18:49:27            780   
1  2017-06-12 19:00:33  2017-06-12 19:24:22           1429   
2  2017-02-13 17:02:02  2017-02-13 17:20:10           1088   
3  2017-04-24 18:39:45  2017-04-24 18:54:59            914   
4  2017-01-26 15:36:07  2017-01-26 15:43:21            434   

              Start Station                          End Station  \
0     Columbus Dr & Randolph St                 Federal St & Polk St   
1        Kingsbury St & Erie St  Orleans St & Merchandise Mart Plaza   
2         Canal St & Madison St              Paulina Ave & North Ave   
3  Spaulding Ave & Armitage Ave       California Ave & Milwaukee Ave   
4        Clark St & Randolph St         Financial Pl & Congress Pkwy   

    User Type  Gender  Birth Year  
0  Subscriber    Male      1991.0  
1    Customer     NaN         NaN  
2  Subscriber  Female      1982.0  
3  Subscriber    Male      1966.0  
4  Subscriber  Female      1983.0   
tobsecret

I think you don't need to go through all the trouble of reading in files with the csv module first. You are also re-assigning csvReader two times, so the first two files (Chicago and New York) are not referred to by anything after you are done reading in csv files.

Below is the pandas way of reading in multiple files and combining them into one file:

import pandas as pd
import os

city_data_files = ['C:\\Users\\jasch\\chicago.csv','C:\\Users\\jasch\\new_york_city.csv', 'C:\\Users\\jasch\\washington.csv']

In this line below, we are looping through the list of file paths and creating a DataFrame for each one, leaving us with a list of DataFrames. Additionally we are using the .assign() method to add a column with the filename. We do this so after combining the DataFrames together we can still tell apart which row came from which file.

dfs = [
       pd.read_csv(city_data_file, parse_dates=['Start Time'])\
       .assign(filename=os.path.basename(city_data_file)) 
       for city_data_file in city_data_files
       ]

Now we can go ahead and combine all the DataFrames into one DataFrame.

df = pd.concat(dfs) # this line combines the contents of the files 
df['month'] = df['Start Time'].dt.month

As for your error - the stack trace is telling you exactly what the problem is:

----> 9 df = pd.read_csv(CITY_DATA[city])
NameError: name 'city' is not defined

You are using the variable city but have never defined it anywhere in your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas data frame from dictionary

From Dev

Pull data from Tableau Server into Pandas Dataframe

From Dev

How to create a dictionary of a dictionary of a dictionary from a pandas data frame

From Dev

Object Defined Error in Data from Internet Pull

From Dev

Object Defined Error in Data from Internet Pull

From Java

SSL Error When installing rubygems, Unable to pull data from 'https://rubygems.org/

From Dev

Remove duplicate pandas data frames from dictionary

From Dev

Nested dictionary from pandas data frame

From Dev

How to make a dictionary from the pandas data frame?

From Dev

Pandas creating data frame from matrix in dictionary

From Dev

Pandas: How to combine ix slice with data from other data frame

From Dev

How to use a path data from a resource dictionary in UWP

From Dev

combine data in pandas

From Dev

Django ManyToManyField error when trying to pull values from other models

From Dev

Using CURL to Pull Data From CRM API Throwing Error

From Dev

Syntax error in Sql while trying to pull data from other table

From Dev

pull data from a file

From Dev

Combine path from a list of objects

From Dev

Converting dictionary of dictionary of dictionary to pandas data frame

From Dev

"The given path's format is not supported" Error when trying to pull XML into Treeview

From Dev

How to create multiple value dictionary from pandas data frame

From Dev

Pandas data frame to dictionary with two keys from index and columns

From Dev

Add a new column to pandas data frame from dictionary

From Dev

How to pull out values from a dictionary into an array

From Dev

Dictionary from Pandas dataframe

From Dev

Swift - Error Accessing Data From Dictionary with Array of Dictionaries

From Dev

Error while fetching data from sorted dictionary in C#

From Dev

WPF - not refreshing data in DataGrid when i remove items from dictionary

From Dev

Error when importing Pandas from python canopy

Related Related

  1. 1

    Pandas data frame from dictionary

  2. 2

    Pull data from Tableau Server into Pandas Dataframe

  3. 3

    How to create a dictionary of a dictionary of a dictionary from a pandas data frame

  4. 4

    Object Defined Error in Data from Internet Pull

  5. 5

    Object Defined Error in Data from Internet Pull

  6. 6

    SSL Error When installing rubygems, Unable to pull data from 'https://rubygems.org/

  7. 7

    Remove duplicate pandas data frames from dictionary

  8. 8

    Nested dictionary from pandas data frame

  9. 9

    How to make a dictionary from the pandas data frame?

  10. 10

    Pandas creating data frame from matrix in dictionary

  11. 11

    Pandas: How to combine ix slice with data from other data frame

  12. 12

    How to use a path data from a resource dictionary in UWP

  13. 13

    combine data in pandas

  14. 14

    Django ManyToManyField error when trying to pull values from other models

  15. 15

    Using CURL to Pull Data From CRM API Throwing Error

  16. 16

    Syntax error in Sql while trying to pull data from other table

  17. 17

    pull data from a file

  18. 18

    Combine path from a list of objects

  19. 19

    Converting dictionary of dictionary of dictionary to pandas data frame

  20. 20

    "The given path's format is not supported" Error when trying to pull XML into Treeview

  21. 21

    How to create multiple value dictionary from pandas data frame

  22. 22

    Pandas data frame to dictionary with two keys from index and columns

  23. 23

    Add a new column to pandas data frame from dictionary

  24. 24

    How to pull out values from a dictionary into an array

  25. 25

    Dictionary from Pandas dataframe

  26. 26

    Swift - Error Accessing Data From Dictionary with Array of Dictionaries

  27. 27

    Error while fetching data from sorted dictionary in C#

  28. 28

    WPF - not refreshing data in DataGrid when i remove items from dictionary

  29. 29

    Error when importing Pandas from python canopy

HotTag

Archive