K.S
K.S

Reputation: 113

How to match values from one column to second column with multiple values

I have a dataframe:

Name  Dept
abc   Genteic|Biology|Chemical Engineering
def   Physics|Chemical Engineering|Astrophysics
xyz   Chemical Engineering|Astrophysics
klm   Biology|Astrophysics
nop   Chemical Engineering|Astrophysics

The first column contains name and second column shows the various departments they are associated with. I want to know number of people working in each department. For ex: In biology dept how many people are associated with. The code i have so for is :

import  pandas as pd
import json
import requests
from requests.exceptions import ConnectionError
from requests.exceptions import ReadTimeout
import csv

def author_name(dataframe):
      response = get_url(term)
      return response

def get_url(term):
print(term)
response = resp.content
data = json.loads(response)
print(data) 

try:
    if data['author-retrieval-response']['subject-areas']['subject-area'] != 'null':
        myvar = data['author-retrieval-response']['subject-areas']['subject-area']['@abbrev']
        myvar = myvar.split('|')

    else:
        data['author-retrieval-response']['subject-areas']['subject-area'] = 'null'
        auth_empty =  data['author-retrieval-response']['subject-areas']['subject-area']['@abbrev']
        print(auth_empty)
except:
    pass

if __name__ =='__main__': 

out = open('out.csv', 'w',encoding='utf-8', newline="\n")
csvwriter = csv.writer(out)
header = ['Scopus ID', 'Title', 'Abstract', 'Affilaition', 'Authors', 
'Citation', 'Pub_Date']       

dataframe = pd.read_csv('author.csv', usecols='auth_name')
for i, row in dataframe.iterrows():
      term = (str(row[0]))
      response = author_name(dataframe)
      csvwriter.writerow(response)

Any help will be greatly appreciated. Thanks !!

Upvotes: 0

Views: 67

Answers (1)

Tom Mekken
Tom Mekken

Reputation: 1039

I wrote you a very simple pythonscript that does, what I think you want it to do. I ignored that fact that the inputfile is a csv-file, and that there do exist libraries for parsing it. The following is just a quick and dirty solution, to hint you into the right direction. I would recommend you to improve this snippet:

  • use a csv-library to process the file
  • use a kind of dictionary for your variables edit: already done
  • try to get rid of the string-comparison (p.e. use the subject as a key for your dictionary) edit: already done

input.csv

abc   Genteic|Biology|Chemical Engineering
def   Physics|Chemical Engineering|Astrophysics
xyz   Chemical Engineering|Astrophysics
klm   Biology|Astrophysics
nop   Chemical Engineering|Astrophysics

main.py

counters = {"Biology":0, "Genteic":0, "Chemical Engineering":0, "Physics":0, "Astrophysics":0}

csv_file = open("input.csv", "r")

for line in csv_file.read().splitlines():
    arr=line.split("   ")
    name=arr[0]
    professions=arr[1]
    for subj in professions.split("|"):
        counters[subj] += 1

csv_file.close()
print("There are %s teachers working in Biology" % counters["Biology"])
print("There are %s teachers working in Genteic" % counters["Genteic"])
print("There are %s teachers working in Chemical Engineering" % counters["Chemical Engineering"])
print("There are %s teachers working in Physics" % counters["Physics"])
print("There are %s teachers working in Astrophysics" % counters["Astrophysics"])

call of python3 main.py results in:

There are 2 teachers working in Biology
There are 1 teachers working in Genteic
There are 4 teachers working in Chemical Engineering
There are 1 teachers working in Physics
There are 4 teachers working in Astrophysics

Upvotes: 1

Related Questions