in python how would you find a specific column you want with

in python, how would you find a specific column you want within multiple columns. For example, we have a csv file that has 4 columns with: restaurant, zip code ,neighborhood , and district. in the restaurant column, i want to find all \"Sonics\". Then, i want to find out which district has more than one \"sonic\". Apologies for the confusion. Please help in providing code for this issue. Thank you.

Solution

Edit 1: CSV is comma separated value. Use:

col=line.strip().split(\",\")

----------------------------------------------------------------------------

If the data is separated by single space. Use:

col=line.strip().split(\" \")

If the data is separated by single tab, Use:

col=line.strip().split(\"\\t\")

---------------------------------------------------------------------

Here is the sample data.csv file (columns separated by space), that I have used for testing. You could replace your original file with this.

Sonics 111 xyz dist1
Sonics 111 xyz dist1
Sonics 111 xyz dist1
Dummy 222 abc dist2
Sonics 333 efg dist3
Sonics 444 def dist4
Sonics 555 jkl dist5
Dummy 111 mno dist1
Dummy 777 pqr dist7
Dummy 222 stu dist2
Sonics 666 vwx dist6
Sonics 333 efg dist3
Sonics 333 efg dist3
Sonics 333 efg dist3

---------------------------------------------------------------------

Python Code (Comments are already added to explain the code)

#import default dictionary
from collections import defaultdict

#Open the csv file
fdata=open(\"data.csv\")

#data is a default dictionary contains list
data = defaultdict(list)

#count the number of Sonics
scount = 0

#district stores the number of Sonics
#it is dictionary with key as district name and value as count of Sonic
district= defaultdict()

#for each line in csv file
for line in fdata:
col=line.strip().split(\" \") #assume each column is separated by one space

rest = col[0].strip().split(None,1) #read first column in a line
dist = col[3].strip().split(None,1) #read third column in a line

#if first column is Sonics
if(rest[0]==\"Sonics\"):
    scount+=1; #increment the Sonics sount
    try:
      district[dist[0]]=district[dist[0]]+1 #increment the district[name_of_district] by 1
    except:
      district[dist[0]]=1 #first initialisation
else: #if no Sonic
    try:
      district[dist[0]]=district[dist[0]] #no change in count
    except:
      district[dist[0]]=0 #first initialisation. No \"Sonics\" count will be 0
  
print(\"Total number of Sonics: %d\" %scount)

print(\"Districts with more than 1 Sonics: \")
#print district for each key(district) in district if Sonics count is more than 1
for key in district:
if(district[key]>1):
    print(\"%s \" %key)

---------------------------------------------------------------------

Output for the sample data.csv

Total number of Sonics: 10
Districts with more than 1 Sonics:
dist1
dist3

in python, how would you find a specific column you want within multiple columns. For example, we have a csv file that has 4 columns with: restaurant, zip code
in python, how would you find a specific column you want within multiple columns. For example, we have a csv file that has 4 columns with: restaurant, zip code

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site