dictionary

how to group by keys with respect of some part of values in python?

浪尽此生 提交于 2021-01-29 09:00:22
问题 I have a json file with keys and Results . The result has 3 parts. like this: df.json()[0]['Results'][0] {'categoryId': '2674', 'categoryName': 'software engineer', 'score': '1.0377672767639161'} I want to collect all keys who have the same categoryName in the result. and then count them. Is it possible? 回答1: One way of doing it would be iterating over every group in one of the following ways: if you just want to know the count of one group you can do this: category_name = "software engineer"

Python | Dictionary Formatting with Azure Face API

最后都变了- 提交于 2021-01-29 08:35:09
问题 I have this which looks like a dictionary but isn't: {'additional_properties': {}, 'anger': 0.001, 'contempt': 0.002, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.542, 'neutral': 0.455, 'sadness': 0.0, 'surprise': 0.0} I tried this solution to fix it Given: d = {'additional_properties': {}, 'anger': 0.001, 'contempt': 0.002, 'disgust': 0.0, 'fear': 0.0, 'happiness': 0.542, 'neutral': 0.455, 'sadness': 0.0, 'surprise': 0.0} Formater: print(', '.join([f'{v:.0%} {k}' for k,v in d.items() if k !=

python dictionary of list of list aggregate

怎甘沉沦 提交于 2021-01-29 08:05:59
问题 I have timeseries dictionary available, I need to count all the values of each keys, what's the most efficient way to do this? DATA = {u'604': [[1361836800, {u'14885549': 52, u'91478624': 127, u'25581439': 12, u'532617990': 4}], [1361833200, {u'14885549': 38, u'91478624': 204, u'25581439': 14, u'40302362': 5, u'532617990': 2}]]} My attempt to do this is here (which is surely idiotic but works): total = 0 for i in DATA: for j in DATA[i]: for k in j[1]: total += j[1][k] Please help? 回答1: Using

Creating dataframe from Nested Dictionary [duplicate]

佐手、 提交于 2021-01-29 08:02:40
问题 This question already has answers here : Construct pandas DataFrame from items in nested dictionary (6 answers) Closed 2 years ago . I am calling an API that returns a batch request of multiple stock tickers in JSON format. It is a nested dictionary, with 2 levels of keys and then a list of dictionaries. Here is the script: import json import requests import pandas as pd r = requests.get('https://api.iextrading.com/1.0/stock/market/batch?symbols=aapl,wpx,mnro,twnk,labl,plnt,fsct,qyls,vrns

Pandas Dataframe splitting a column with dict values into columns

£可爱£侵袭症+ 提交于 2021-01-29 08:01:04
问题 I am attempting to split and convert a column, in a pandas dataframe, with list of dictionary values into a new columns. Using Splitting dictionary/list inside a Pandas Column into Separate Columns as a reference things appear to fail because some of the values are NaN. When these rows are encountered an error is thrown, can't iterate over float and if I fillna with None the error changes to a str related error. I have attempted to first use: df.explode('freshness_grades') df_new = pd.concat(

How to know if a value is inside a nested dictionary?

房东的猫 提交于 2021-01-29 06:51:22
问题 I'm new to Python and I'm still learning how to use. I have the follow dictionary: dic = {'0': {'text': 'a', 'lang': 'es', 'rating': '4'}, '1': {'text': 'b', 'lang': 'es', 'rating': '3'}, '2': {'text': 'c', 'lang': 'es', 'rating': '1'}, '3': {'text': 'd', 'lang': 'es', 'rating': '2'}, '4': {'text': 'e', 'lang': 'es', 'rating': '5'}} Now, I'm trying to know if the text, for example, 'a' is a value of any of those nested dictionaries (I know there is a value() function which returns the values

How to define a variable being a dictionary with list values in Robot Framework

ⅰ亾dé卋堺 提交于 2021-01-29 06:14:54
问题 In one of my testcases I need to define a dictionary, where the keys are string and the values are arrays of strings. How can I do so in Robot Framework? My first try using a construct as shown below, will not work. *** Variables *** &{Dictionary} A=StringA1 StringA2 ... B=StringB1 StringB2 Another idea might be to use Evaluate and pass the python expression for a dictionary, but is this the only way how it can done? *** Variables *** &{Dictionary} Evaluate { "A" : ["StringA1", "StringA2"],

C# Check if dictionary contains key which is a reference type [duplicate]

我是研究僧i 提交于 2021-01-29 06:11:25
问题 This question already has answers here : An integer array as a key for Dictionary (3 answers) Closed 6 months ago . If I have a Dictionary<int[], int> , which contains 2 entries {1, 2, 3} , 1 and {4, 5, 6} , 2 How do I check whether the dictionary contains the key {1, 2, 3} ? If i do: if (dictionary.ContainsKey(new int[] {1,2,3}) { // do things } It will not return the correct result as the created array is different from the key array. I know you can override the Equals method for a custom

Python: Create directory tree from nested list of dictionaries

你离开我真会死。 提交于 2021-01-29 05:29:24
问题 How can I create a directory tree from the below list of dictionaries in python? The number of subdirectory levels has to be variable. dirTree: [{'level-1_dir-1': ''}, {'level-1_dir-2': [{'level-2_dir-1': [{'level-3_dir-1': ''}, {'level-3_dir-2': ''}, {'level-3_dir-3': ''} ] }, {'level-2_dir-2': ''} ] }, {'level-1_dir-3': ''}, {'level-1_dir-4': ''} ] I would like to accomplish something like the following tasks with python: Iterate through the level-1 keys create folder with key name if it

Rearranging a dictionary based on a function-condition over its items

两盒软妹~` 提交于 2021-01-29 05:28:47
问题 (In relation to this question I posed a few days ago) I have a dictionary whose keys are strings, and whose values are sets of integers, for example: db = {"a":{1,2,3}, "b":{5,6,7}, "c":{2,5,4}, "d":{8,11,10,18}, "e":{0,3,2}} I would like to have a procedure that joins the keys whose values satisfy a certain generic condition given in an external function. The new item will therefore have as a key the union of both keys (the order is not important). The value will be determined by the