I have a data like
sql = "select value, type from table"
cur.execute(sql)
row_headers = [x[0] for x in cur.description] #this will extract row headers
rv = cur.fetchall()
json_result = []
for result in rv:
json_result.append(dict(zip(row_headers, result)))
finalresult = json.dumps(json_result)
#[{'value':30,'type':'SS'},{'value':40,'type':'KK'},{'value':80,'type':'SK'}]
attrList = list(map(lambda x: x['value'], finalresult))
return attrList
I want OP :
[30,40,80]
But I am getting ERROR :
attrList = list(map(lambda x: x['value'], finalresult))
TypeError: string indices must be integers
Where am I doing mistake?
Related
I would like to access and edit individual dataframes after creating them by a for loop.
#Let's get those files!!!
bdc_files = {'nwhl': 'https://raw.githubusercontent.com/bigdatacup/Big-Data-Cup-2021/main/hackathon_nwhl.csv',
'olympics': 'https://raw.githubusercontent.com/bigdatacup/Big-Data-Cup-2021/main/hackathon_womens.csv',
'erie': 'https://raw.githubusercontent.com/bigdatacup/Big-Data-Cup-2021/main/hackathon_scouting.csv'}
df_list = []
for (a,b) in bdc_files.items():
#Grab csv file
c = pd.read_csv(b)
c.name = a
#a = a.append(c)
#Manipuling the Data as we please
c['Game_ID'] = c['game_date'] + c['Home Team'] + c['Away Team']
c['Detail 3'] = c['Detail 3'].replace('t', 'with traffic')
c['Detail 3'] = c['Detail 3'].replace('f', 'without traffic')
c['Detail 4'] = c['Detail 4'].replace('t', 'one-timer')
c['Detail 4'] = c['Detail 4'].replace('f', 'not one-timer')
c['Details'] = c['Detail 1'].astype(str).add(' ').add(c['Detail 2'].astype(str)).add(' ').add(c['Detail 3'].astype(str)).add(' ').add(c['Detail 4'].astype(str))
c['is_goal'] = 0
c['is_shot'] = 0
c.loc[c['Event'] == 'Shot', 'is_shot'] = 1
c.loc[c['Event'] == 'Goal', 'is_goal'] = 1
c['Goal Differential'] = c['Home Team Goals'] - c['Away Team Goals']
c['Clock'] = pd.to_datetime(c['Clock'], format = '%M:%S')
c['Seconds Remaining'] = ((c['Clock'].dt.minute)*60) + (c['Clock'].dt.second)
df_list.append(a)
#Printing Datasheet info
Title = "The sample of games from the {}".format(c.name)
print(c.name)
print(Title + " dataset is:", len(list(c['Game_ID'].value_counts())))
print(c['Event'].value_counts())
print(c.columns.values)
print(c.loc[c['Event'] == 'Shot', 'Details'].value_counts())
print(c.head())
print(c.info())
print(df_list)
print(nwhl)
However, if I want to print the nwhl database, I get the following output...
Empty DataFrame
Columns: []
Index: []
And if I were to use an append, I would get this error
AttributeError: 'str' object has no attribute 'append'
Long story short, based off of the code I have, how can I be able to print and perform other tasks with the dataframes outside of the for loop? Any assistance is truly appreciated.
Use a dictionary of dataframes, df_dict:
Add
df_dict = {}
...
for (a,b) in bdc_files.items():
#Grab csv file
c = pd.read_csv(b)
c.name = a
# Add this line to build dictionary
df_dict[a] = c
And, at the end print
df_dict['nwhl']
I have been trying to fetch recommendation details from API Like top 5 matching
Colab code for in detail understanding
cached based API calling
Input : Smile value : O=C(N1C=CN=C1)NC2=NC(C)=C(C3=CC=C(S(=O)(C)=O)C(F)=C3)S2
output
Reac = NC1=CSC=C1C(OC)=O.O=C(Cl)CCl Reag = CCN-CC.C(Cl)Cl
Reac = NC1=CSC=C1C(OC)=O.O=C(Cl)CCl Reag = []
Reac = NC1=CSC=C1C(OC)=O.O=C(Cl)CCl Reag = CCN-CC.C(Cl)Cl
Reac = NC1=CSC=C1C(OC)=O.O=C(Cl)CCl Reag = []
This is value
reac - ['O=C(OC(C)(C)C)NCCC(NC(C)(C)C)=O', 'O=[C]C(F)(F)F']
reag - ['']
While got Empty value in reag variable it throws error message
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
My actual logic is here
from cachetools import cached
from diskcache import Cache
BASE_URL = "https://ai.chemistryinthecloud.com/smilies/"
CACHEDIR = "api_cache"
#cached(cache=Cache(CACHEDIR))
def get_similies(value):
return requests.get(BASE_URL + value).json()
def parse_eq(eq):
reac, _, reag = (list(v.split(".")) for v in eq.partition(">"))
print("reac -",reac)
print("reag -",reag)
print(type(reag[0]))
return {
"reactants": {i: get_similies(i) for i in reac},
"reagents": {i: get_similies(i) for i in reag if i is not None}
}
result = [parse_eq(eqs) for eqs in l]
How Can I skip the empty value and go to the next loop value?
I have list
keywords = ['car','bike','motorcycle', 'airplane', 'boat', 'boat']
and I have a api that returns data
[('bike','http://bikes.com/bikes'),
('boat':'http://boat.com/boat')]
so I need a tuple/list which can be like
[('car','none'),
('bike','http://bikes.com/bikes'),
('motorcycle','none'),
('airplane','none'),
('boat','http://boat.com/boat'),
('boat','http://boat.com/boat')]
Basically I was able to create with dictionary how ever I wanted to retain duplicates.
keyword_list = keyword.split(",")
response = requests.request("GET", url, headers=headers, params=querystring)
field_json = json.loads(response.text)
data = field_json['response']
field_json = json.loads(response.text)
data = field_json['response']
if len(data)> 0:
list1 = {}
for i in data['docs']:
list1[i['keyword_s']]= i['url_s']
print(list(list1.items()))
keyword_dict = {k: None for k in keyword_list}
keyword_dict.update(list1)
return keyword_dict
Your code:
keywords = ['car','bike','motorcycle', 'airplane', 'boat', 'boat']
api = [('bike','http://bikes.com/bikes'), ('boat', 'http://boat.com/boat')]
Solution:
api_dict = dict(api)
out = [api_dict.get(k) for k in keywords]
api = [('bike','http://bikes.com/bikes'),
('boat','http://boat.com/boat')]
keywords = ['car','bike','motorcycle', 'airplane', 'boat', 'boat']
ans = []
missing = set(keywords)
for k, v in api:
if k in keywords:
ans.append((k, v))
missing.remove(k)
for miss in missing:
ans.append((miss, None))
print(ans)
I am using the function below to compute inverse term frequencies. I am trying to call function. total_all is a list
inverse_document_frequencies(total_all)
def inverse_document_frequencies(tokenized_documents):
idf_values = {}
global dfInv
dfInv = pd.DataFrame()
tokenized_documents = [tokenize(d) for d in words]
all_tokens_set = set([item for sublist in tokenized_documents for item in
sublist])
for tkn in all_tokens_set:
contains_token = len(list(map(lambda doc: tkn in doc, tokenized_documents)))
idf_values[tkn] = 1 + math.log(len(tokenized_documents)/(sum(contains_token)))
dfInv=pd.DataFrame(data ={'tkn':contains_token,'idf_values':idf_values[tkn]})
return dfInv
I have added parantheses and changed indentation, and this helps get rid of the syntax error. I am getting an error below on calling the function.
typeerror 'int' object is not iterable.
No Syntax error in following code. It was all about indentation and missing parenthesis
def inverse_document_frequencies(tokenized_documents):
idf_values = {}
global dfInv
dfInv = pd.DataFrame()
tokenized_documents = [tokenize(d) for d in words]
all_tokens_set = set([item for sublist in tokenized_documents for item in
sublist])
for tkn in all_tokens_set:
contains_token = len(list(map(lambda doc: tkn in doc, tokenized_documents)))
idf_values[tkn] = 1 + math.log(len(tokenized_documents)/(sum(contains_token)))
dfInv=pd.DataFrame({'tkn':contains_token,'idf_values':idf_values[tkn]})
return dfInv
I cannot figure how to return all the items using this code:
#staticmethod
def create_dataset():
cols = Colleagues.get_all_colleagues()
cols_abs = ((col['Firstname'] + " " + col['Surname'], col['Absences']) for col in cols)
for col in cols_abs:
dataset = list()
sum_days = list()
for d in col[1]:
start_date = d[0]
end_date = d[1]
s = datetime.strptime(start_date, "%Y-%m-%d")
e = datetime.strptime(end_date, "%Y-%m-%d")
startdate = s.strftime("%b-%y")
days = numpy.busday_count(s, e) + 1
sum_days.append(days)
days_per_month = startdate, days
dataset.append(days_per_month)
dict_gen1 = dict(dataset)
comb_days = sum(sum_days)
dict_gen2 = {'Name': col[0], 'Spells': len(col[1]), 'Total(Days)': comb_days}
dict_comb = [{**dict_gen1, **dict_gen2}]
return dict_comb
It only returns the first "col". If I move the return statement outside of the loop it returns only the last item in my set of data. This is the output that is returned from col_abs:
('Jonny Briggs', [['2015-08-01', '2015-08-05'], ['2015-11-02', '2015-11-06'], ['2016-01-06', '2016-01-08'], ['2016-03-07', '2016-03-11']])
('Matt Monroe[['2015-12-08', '2015-12-11'], ['2016-05-23', '2016-05-26']])
('Marcia Jones', [['2016-02-02', '2016-02-04']])
('Pat Collins', [])
('Sofia Marowzich', [['2015-10-21', '2015-10-30'], ['2016-03-09', '2016-03-24']])
('Mickey Quinn', [['2016-06-06', '2016-06-08'], ['2016-01-18', '2016-01-21'], ['2016-07-21', '2016-07-22']])
('Jenifer Andersson', [])
('Jon Fletcher', [])
('James Gray', [['2016-04-01', '2016-04-06'], ['2016-07-04', '2016-07-07']])
('Matt Chambers', [['2016-05-02', '2016-05-04']])
Can anyone help me understand this better as I want to return a "dict_comb" for each entry in col_abs ?
Replace your return statement with a yield statement. This will allow your method to continue to loop while "yielding" or returning values after each iteration.