How to match and assign data the pythonic way? - mysql

I have a list (mysql table) of People and their titles as shown in the table below. I also have a list of titles and their categories. How do I assign their categories to the person? The problem arises when there are multiple titles for a person. What is the pythonic way of mapping the title to the category and assigning it to the person?
People Table
Name Title
--------------------
John D CEO, COO, CTO
Mary J COO, MD
Tim C Dev Ops, Director
Title Category table
Title Executive IT Other
-----------------------------
CEO 1
COO 1
CTO 1 1
MD 1
Dev Ops 1
Director 1
Desired output :
Name Title Executive IT Other
---------------------------------------------
John D CEO, COO, CTO 1 1
Mary J COO, MD 1
Tim C Dev Ops, Director 1 1

name_title = (("John D",("CEO","COO","CTO")),
("Mary J",("COO","MD")),
("Tim C",("Dev Ops","Director")))
title_cat = {"CEO": set(["Executive"]),
"COO": set(["Executive"]),
"CTO": set(["Executive"]),
"MD": set(["Executive"]),
"Dev Ops": set(["IT"]),
"Director": set(["Other"])}
name_cat = [(name, reduce(lambda x,y:x|y, [title_cat[title]for title in titles])) for name,titles in name_title]
It would be nice if there was a union which behaved like sum on sets.

people=['john','Mary','Tim']
Title=[['CEO','COO','CTO'],['COO','MD'],['DevOps','Director']]
title_des={'CEO':'Executive','COO':'Executive','CTO':'Executive',
'MD':'Executive','DevOps':'IT','Director':'Others'
}
people_des={}
for i,x in enumerate(people):
people_des[x]={}
for y in Title[i]:
if title_des[y] not in people_des[x]:
people_des[x][title_des[y]]=[y]
else:
people_des[x][title_des[y]].append(y)
print(people_des)
output:
{'Tim': {'IT': ['DevOps'], 'Others': ['Director']}, 'john': {'Executive': ['CEO', 'COO', 'CTO']}, 'Mary': {'Executive': ['COO', 'MD']}}

Start by arranging your input data in a dictionary-of-lists form:
>>> name_to_titles = {
'John D': ['CEO', 'COO', 'CTO'],
'Mary J': ['COO', 'MD'],
'Tim C': ['Dev Ops', 'Director']
}
Then loop over the input dictionary to create the reverse mapping:
>>> title_to_names = {}
>>> for name, titles in name_to_titles.items():
for title in titles:
title_to_names.setdefault(title, []).append(name)
>>> import pprint
>>> pprint.pprint(title_to_names)
{'CEO': ['John D'],
'COO': ['John D', 'Mary J'],
'CTO': ['John D'],
'Dev Ops': ['Tim C'],
'Director': ['Tim C'],
'MD': ['Mary J']}

I propose this if you mean you have the string:
s = '''Name Title
--------------------
John D CEO, COO, CTO
Mary J COO, MD
Tim C Dev Ops, Director
Title Executive IT Other
-----------------------------
CEO 1
COO 1
CTO 1
MD 1
Dev Ops 1
Director 1
'''
lines = s.split('\n')
it = iter(lines)
for line in it:
if line.startswith('Name'):
break
next(it) # '--------------------'
for line in it:
if not line:
break
split = line.split()
titles = split[2:]
name = split[:2]
print ' '.join(name), titles
# John D ['CEO,', 'COO,', 'CTO']
# Mary J ['COO,', 'MD']
# Tim C ['Dev', 'Ops,', 'Director']

Related

Calculation script to find average based on other columns in Google Sheets

I have a Google Sheet that is being used to track applicant interview data. I am trying to find the Round Average Score for each candidate based on their Interview Round and Round score. I figured out how to gather this data with a query function but for this use case in particular it has to be done in a script.
Here is an example of the sheet
Any help would be greatly appreciated.
Average of Average Scores
function lfunko() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
let co = { pA: [] }
vs.forEach((r, i) => {
let p = `${r[0]}/${r[2]}`;
if (!co.hasOwnProperty(p)) {
co[p] = { cnt: 1, sum: r[4], idx: i }
co.pA.push(p);
} else {
co[p].cnt += 1;
co[p].sum += r[4];
}
});
let vo = vs.map((r, i) => {
let p = `${r[0]}/${r[2]}`;
if (i == co[p].idx) {
return [co[p].sum / co[p].cnt];
} else {
return [''];
}
})
sh.getRange(2, 6, vo.length, vo[0].length).setValues(vo);
}
Ouput:
Candidate
Position
Interview Round
Panelist
Round Score
Round Average Score
Bob
Tester
First
Jon
3
4
Bob
Tester
First
Janet
4
Bob
Tester
First
Joe
5
Bob
Tester
Second
Sal
4
3.333333333
Bob
Tester
Second
Riley
3
Bob
Tester
Second
Tae
3
Bob
Tester
Final
Wanda
5
4.666666667
Bob
Tester
Final
Kelly
4
Bob
Tester
Final
Arnold
5
Al
Senior Tester
First
Ben
2
3
Al
Senior Tester
First
Tori
3
Al
Senior Tester
First
Harry
4
Al
Senior Tester
Second
Kate
4
3.666666667
Al
Senior Tester
Second
Wendy
5
Al
Senior Tester
Second
Carl
2
Al
Senior Tester
Final
Sam
5
4
Al
Senior Tester
Final
Jake
3
Al
Senior Tester
Final
Troy
4
If you need to get the data as permanent static values that will not change later even if the source data gets modified, you can still use a query() formula to get the results, and then use a short script to replace the formula and its results with static values. To try it out, Insert > Sheet and use this:
=query(sumAve!A1:E, "select A, B, avg(D) where D is not null group by A, B", 1)
/**
* Replaces formulas with values in the active sheet.
*/
function replaceFormulasWithValuesInActiveSheet() {
const wholeSheet = SpreadsheetApp.getActiveSheet().getDataRange();
wholeSheet.setValues(wholeSheet.getValues());
}

select case when in MYSQL

I have 2 tables
First tabel name is "consumer"
id_consumer
name
1
Roy
2
Dori
3
Rico
Second tabel name is "consumer_address"
id_consumer
address
status
1
Street Avenue
1
1
Park Hill
0
2
Highwalk Street
1
2
Albion Place
0
Condition
name from tabel "consumer"
address from "consumer_address" , but i want to get only 1 address when consumer_address.status = 1
When Consumer not have data in tabel "consumer_address", field is NULL
The Final Tabel Like this
id_consumer
name
address
status
1
Roy
Street Avenue
1
2
Dori
Highwalk Street
1
3
Rico
NULL
NULL
i have query, but its not work
this is my query
SELECT
id_consumer,
name,
CASE WHEN (`consumer_address`.`status` = 1) THEN `consumer_address`.`address` ELSE NULL END as "Address",
CASE WHEN (`consumer_address`.`status` = 1) THEN `consumer_address`.`status` ELSE NULL END as "Status"
FROM consumer
JOIN consumer_address ON consumer_address.id_consumer = consumer.id_consumer
Thanks
Very simple solution:
SELECT
`id_consumer`,
`name`,
`consumer_address`.`address`,
`consumer_address`.`status`
FROM consumer
LEFT JOIN consumer_address ON
`consumer_address`.`id_consumer` = `consumer`.`id_consumer` AND
`consumer_address`.`status` = 1
Instead of using CASE WHEN just include the status in the JOIN.
Additionally, to keep consumer 3, you need a LEFT JOIN.
SELECT
id_consumer,
name,
`consumer_address`.`address`,
`consumer_address`.`status`
FROM
consumer
LEFT JOIN
consumer_address
ON consumer_address.id_consumer = consumer.id_consumer
AND consumer_address.status = 1

Transform a CSV of Ids into a CSV of Names

I need to transform a csv of Ids into a csv of Names.
I have:
FOLDER ID NAME | FILE ID NAME PATH
1 A 1 fX 1
2 AB 2 fZ 1,2
3 B 3 fY 3,4
4 BC 4 fW 3,4,5
5 BCD
Get info about FILEs and its sizes from the FILEDATA table
select FILE.NAME, FILE.PATH, FILEDATA.SIZE
from FILEDATA inner join FILE on FILEDATA.fileid = FILE.id
WHERE FILEDATA.PropName = "Size"
Actually I get
fX 1 23805
fZ 1,2 27205
fY 3,4 23608
fW 3,4,5 21501
I need replace the IDs by the FOLDER names
fX A 23805
fZ A/AB 27205
fY B/BC 23608
fW B/BC/BDC 21501

Split New Line - MS Access

Would appreciate any help on this problem
In MS Access
I'd like to split the values of one field (Main Address) to 2 separate fields (Address 1 and Address 2) where in Address 1 gets the first line and then Address 2 gets the second and other line items
ex #1
Main Address | Address 1 | Address 2
----------------------------------------
1 Main Road | 1 Main Road | San Jose CA
San Jose CA
ex #2
Main Address | Address 1 | Address 2
----------------------------------------
1 Main Road | 1 Main Road | San Jose CA Drop at Front
San Jose CA
Drop at Front
Thanks All!
Hope the representation of the samples makes sense, if not let me know if you have questions and I'll clarify! TA
Does the [Main Address] data have Cr and Lf characters to force new lines? If it doesn't, what you want is virtually impossible. If yes, an expression in query or textbox:
Replace(Left([Main Address] & "", Instr([Main Address] & Chr(13), Chr(13))), Chr(13), "")
Trim(Replace(Mid([Main Address] & "", Instr([Main Address] & Chr(13), Chr(13))), Chr(13) & Chr(10), " "))

customized JSON output in pig

Need customized JSON output--
(I have two files - text file and schema file)
abc.txt -
100002030,Tom,peter,eng,block 3, lane 5,california,10021
100003031,Tom,john,doc,block 2, lane 2,california,10021
100004032,Tom,jim,eng,block 1, lane 1,california,10021
100005033,Tom,trek,doc,block 2, lane 2,california,10021
100006034,Tom,peter,eng,block 6, lane 6,california,10021
abc_schema.txt (field name and position)
rollno 1
firstname 2
lastname 3
qualification 4
address1 5
address2 6
city 7
Zipcode 8
Rules-
First 6 characters of rollno
Need to club address1 | address2 | city
Prefix Address to above
Expected Output-
{"rollno":"100002","firstname":"Tom","lastname:"peter","qualification":"eng","Address":"block 3 lane 5 california","zipcode":"10021"}
{"rollno":"100002","firstname":"Tom","lastname:"john","qualification":"doc","Address":"block 2 lane 2 california","zipcode":"10021"}
{"rollno":"100004","firstname":"Tom","lastname:"jim","qualification":"eng","Address":"block 1 lane 1 california","zipcode":"10021"}
{"rollno":"100005","firstname":"Tom","lastname:"trek","qualification":"doc","Address":"block 2 lane 2 california","zipcode":"10021"}
{"rollno":"100006","firstname":"Tom","lastname:"peter","qualification":"eng","Address":"block 6 lane 6 california","zipcode":"10021"}
I do not wish to hardcode the fields but read from the schema file, the idea is to have reusable code. Something like looping schema file and the text file
A = load 'abc.txt' using PigStorage(',') as (rollno, Fname,Lname,qua,add1,add2,city,Zipcode);
B = foreach A generate rollno, Fname,Lname,qua,concate (add1,add2,city) ,Zipcode;
C= STORE B
INTO 'first_table.json'
USING JsonStorage();
Hope this helps.