2017-06-12 6 views
0

Pythonの新機能で、Fire Inspectionデータセットの "Incident_Number、Address、Incident_Date、Primary_Situation、Property_Use、Supervisor_District、 " Neighborhood _District '、Location "フィールドを選択しようとしました。Python:Pythonでcsvから列を選択する方法は?

ここで見つけたコードで試しましたが、無効な構文があります。このコードはここで見つけることができますが、少し修正しました:

import csv 
from collections import defaultdict 

columns = defaultdict(list) # each value in each column is appended to a list 

with open('Fire_Incidents_06052017.csv') as f: 
    reader = csv.DictReader(f) # read rows into a dictionary format 
    for row in reader: # read a row as {column1: value1, column2: value2,...} 
     for (k,v) in row.items(): # go over each column name and value 
      columns[k].append(v) # append the value into the appropriate list 
           # based on column name k 
#Incident_Date, Primary_Situation, Property_Use, Supervisor_District, "Neighborhood _District", Location 

print(columns['Incident_Number']) 
print(columns['Address']) 
print(columns['Incident_Date']) 
print(columns['Primary_Situation']) 
print(columns['Property_Use']) 
print(columns['Supervisor_District']) 
print(columns['Neighborhood _District']) 
print(columns['Location']) 

これらの列をPythonで簡単に取り出す方法はありますか? csvはexcelとSQLiteがクラッシュすることなく開く/エクスポートするには大きすぎます。

+2

'pandas.read_csv()'を試しましたか? – cardamom

+0

興味のある列だけをインポートしたいのですか、またはすべてをインポートしてから、必要な列のサブセットをサブセット化しても問題ありませんか? – GlennFriesen

+0

ようこそ。あなたの問題を突きつけようとしています - あなたは大きなCSVを持っていて、あなたはPythonを読んでいるようです。あなたの最終的な結果は何を望んでいますか?あなたの目標は何ですか?猫をスキンする方法は常に異なります。 –

答えて

0

少しの検索でFire Incidents list for SFのリストに導かれました。

Incident Number,Exposure Number,Address,Incident Date,Call Number,Alarm DtTm,Arrival DtTm,Close DtTm,City,Zipcode,Battalion,Station Area,Box,Suppression Units,Suppression Personnel,EMS Units,EMS Personnel,Other Units,Other Personnel,First Unit On Scene,Estimated Property Loss,Estimated Contents Loss,Fire Fatalities,Fire Injuries,Civilian Fatalities,Civilian Injuries,Number of Alarms,Primary Situation,Mutual Aid,Action Taken Primary,Action Taken Secondary,Action Taken Other,Detector Alerted Occupants,Property Use,Area of Fire Origin,Ignition Cause,Ignition Factor Primary,Ignition Factor Secondary,Heat Source,Item First Ignited,Human Factors Associated with Ignition,Structure Type,Structure Status,Floor of Fire Origin,Fire Spread,No Flame Spead,Number of floors with minimum damage,Number of floors with significant damage,Number of floors with heavy damage,Number of floors with extreme damage,Detectors Present,Detector Type,Detector Operation,Detector Effectiveness,Detector Failure Reason,Automatic Extinguishing System Present,Automatic Extinguishing Sytem Type,Automatic Extinguishing Sytem Perfomance,Automatic Extinguishing Sytem Failure Reason,Number of Sprinkler Heads Operating,Supervisor District,Neighborhood District,Location 
09030109,0,310 Colon Av.,04/12/2009,091020273,04/12/2009 06:09:13 PM,04/12/2009 06:13:45 PM,04/12/2009 07:23:13 PM,SF,,B09,15,,1,5,0,0,0,0,T15,,,0,0,0,0,,551 - assist pd or other govern. agency,none,52 - forcible entry,-,-,-,"000 - property use, other",,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
13067402,0,20 Lansdale Av,07/18/2013,131990117,07/18/2013 10:32:03 AM,07/18/2013 10:37:15 AM,07/18/2013 10:39:55 AM,SF,,B09,39,8571,3,11,0,0,0,0,E39,,,0,0,0,0,,745 - alarm system sounded/no fire-accidental,none,86 - investigate,-,-,-,429 - multifamily dwellings,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
12044490,0,7th St./Folsom St.,05/13/2012,121340051,05/13/2012 03:55:37 AM,05/13/2012 04:01:57 AM,05/13/2012 04:05:44 AM,SF,94103,B03,01,,3,10,0,0,0,0,B03,,,0,0,0,0,,"711 - municipal alarm system, street box false",none,86 - investigate,-,-,-,963 - street or road in commercial area,,,,,,,,,,,,,,,,,,,,,,,,,,,6,South of Market,"(37.7767460000297, -122.407844)" 
13033326,0,2799 Pacific Av,04/09/2013,130990286,04/09/2013 04:34:07 PM,04/09/2013 04:39:31 PM,04/09/2013 05:20:27 PM,SF,,B04,10,4163,3,10,0,0,0,0,B04,,,0,0,0,0,,"746 - co detector activation, no co",none,86 - investigate,-,-,-,419 - 1 or 2 family dwelling,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 
11101416,0,Polk St./Pine St.,11/01/2011,113050357,11/01/2011 06:07:45 PM,11/01/2011 06:10:17 PM,11/01/2011 06:11:09 PM,SF,94109,B04,03,,2,9,0,0,0,0,E03,,,0,0,0,0,,"711 - municipal alarm system, street box false",none,86 - investigate,-,-,-,"960 - street, other",,,,,,,,,,,,,,,,,,,,,,,,,,,3,Nob Hill,"(37.7896190000297, -122.420497)" 
12077793,0,2824 Scott St.,08/22/2012,122350085,08/22/2012 08:58:06 AM,08/22/2012 09:10:06 AM,08/22/2012 09:13:18 AM,SF,94123,B04,16,,3,11,0,0,0,0,E51,,,0,0,0,0,,"700 - false alarm or false call, other",none,86 - investigate,-,-,-,"400 - residential, other",,,,,,,,,,,,,,,,,,,,,,,,,,,2,Marina,"(37.7960234800297, -122.43998326)" 
07021713,0,San Bruno Av./Teddy Av.,03/15/2007,070740223,03/15/2007 01:55:35 PM,03/15/2007 01:59:24 PM,03/15/2007 01:59:56 PM,SF,94134,B10,44,,1,4,0,0,0,0,E42,,,0,0,0,0,,"711 - municipal alarm system, street box false",none,86 - investigate,-,-,-,"962 - residential street, road or residential dr",,,,,,,,,,,,,,,,,,,,,,,,,,,10,Visitacion Valley,"(37.7129950000296, -122.4021)" 
15065212,0,1532 Noriega Street,06/20/2015,151710341,06/20/2015 02:44:56 AM,06/20/2015 02:51:39 AM,06/20/2015 03:18:48 AM,San Francisco,94122,B08,40,7431,8,28,1,2,1,2,,800,,0,0,0,0,,"113 cooking fire, confined to container",n none,52 forcible entry,,,2 detector did not alert occupants,161 restaurant or cafeteria,,,,,,,,,,,,na,,,,,,,,,,,,,,,4,Sunset/Parkside,"(37.7541490000296, -122.480073)" 
10097225,0,1215 Carolina St.,10/31/2010,103040303,10/31/2010 05:14:49 PM,10/31/2010 05:19:50 PM,10/31/2010 05:27:14 PM,SF,94107,B10,37,,3,10,0,0,0,0,E37,,,0,0,0,0,,745 - alarm system sounded/no fire-accidental,none,86 - investigate,-,-,-,"500 - mercantile, business, other",,,,,,,,,,,,,,,,,,,,,,,,,,,10,Potrero Hill,"(37.7533230000297, -122.399403)" 

と、次のコード:

import csv 
columns = {} 
with open("incidents.csv", "r") as f: 
    reader = csv.DictReader(f) 
    for row in reader: 
     for key, value in row.items(): 
      try: 
       columns[key].append(value) 
      except KeyError: 
       columns[key] = [value] 

print(columns['Incident Number']) 

は、次のような出力が得られた:最初の10行使用して、同様のデータ構造をもたらすあなたのコードを、使用

['09030109', '13067402', '12044490', '13033326', '11101416', '12077793', '07021713', '15065212', '10097225'] 

を与えます私は同じ結果です。

注目すべきことは、データセットに似た名前の2つの異なる列があることです。 Neighborhood DistrictおよびNeighborhood District。データが入っている列には2つのスペースがある列が表示されます。

結局のところ、ここで私は、stackoverflowに転送されなかったあなたのコードにある種のタイプミスがあったと結論づけることができます。エラーを完全に投稿してください。

関連する問題