简体   繁体   English

for循环只使用dict中的第一个键

[英]for loop only using 1st key in dict

I'm trying to calculate whether or not an item has been delayed. 我正在尝试计算某件商品是否已延迟。 I have a dictionary containing 20+ keys and values, and am trying to loop through the rows in my dataset, and if the dataframe value is above the dict value, it should result in "True". 我有一个包含20多个键和值的字典,我试图遍历我的数据集中的行,如果数据帧值高于dict值,它应该导致“True”。

It seems like the loop is only using the first value in the dict, and I've tried changing it to no avail. 似乎循环只使用了dict中的第一个值,我尝试将其更改为无效。

This is the current code I'm using: 这是我正在使用的当前代码:

def delaytest(df):
    for key, value in mydict.items():
        if (key == df["serviceType"]) & (value < (df["delayTime"]/60)):
            return True

        else:
            return False

df["delay"] = df.apply(delaytest, axis=1)

Example dict: 示例dict:

dict = {"key1": 5,
        "key2": 10,
        "key3": 15}
df before:
    serviceType   delayTime
    key1          6
    key2          11
    key2          12
    key1          4
    key3          16
    key3          14

df after:
       serviceType   delayTime  delay
    key1          6          True
    key2          11         False
    key2          12         False
    key1          4          False
    key3          16         False
    key3          14         False

df expected:
    serviceType   delayTime  delay
    key1          6          True  
    key2          11         True
    key2          12         True
    key1          4          False
    key3          16         True
    key3          14         False

if the dataframe value is above the dict value, it should result in "True". 如果数据帧值高于dict值,则应该为“True”。

Undestandable. Undestandable。 But in your code, you also return False if you don't hit True . 但是在你的代码中,如果你没有点击True ,你也会返回False So with the first element, you always return something, therefore exiting the function. 因此,对于第一个元素,您总是返回一些东西,因此退出该函数。 ;) ;)

Get rid of the else with the return False and it should work: 使用return False摆脱else ,它应该工作:

def delaytest(df):
    for key, value in mydict.items():
        if (key == df["serviceType"]) & (value < (df["delayTime"]/60)):
            return True

df["delay"] = df.apply(delaytest, axis=1)

I think here is better use Series.map for new Series and compare by column delayTime : 我认为这里最好使用Series.map进行新系列,并按列delayTime进行比较:

df["delay"] = df['serviceType'].map(mydict) < df["delayTime"]
print (df)
  serviceType  delayTime  delay
0        key1          6   True
1        key2         11   True
2        key2         12   True
3        key1          4  False
4        key3         16   True
5        key3         14  False

Detail : 细节

print (df['serviceType'].map(mydict))
0     5
1    10
2    10
3     5
4    15
5    15
Name: serviceType, dtype: int64

If map with value not matched, get missing values and output of comparison are False s: 如果map的值不匹配,则获取缺失值并且比较输出为False

mydict = {"key1": 5,
        "key2": 10,
        "key4": 15}

df["delay"] = df['serviceType'].map(mydict) < df["delayTime"]
print (df)
  serviceType  delayTime  delay
0        key1          6   True
1        key2         11   True
2        key2         12   True
3        key1          4  False
4        key3         16  False
5        key3         14  False

print (df['serviceType'].map(mydict))
0     5.0
1    10.0
2    10.0
3     5.0
4     NaN
5     NaN
Name: serviceType, dtype: float64

EDIT: 编辑:

For possible map by multiple values create helper DataFrame and merge with left join: 对于可能的多个值映射,创建帮助器DataFrame并与左连接merge

pairs = [('cat1','prov1', 'content1'),
         ('cat2','prov2', 'content2'),
         ('cat3','prov3', 'content3')]
df1 = pd.DataFrame(pairs, columns=['category','provider','contentType'])
print (df1)

df = df.merge(df1, how='left')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 循环仅读取文件的第一行 - Loop only reads 1st line of file webscraping selenium:我的循环总是只得到第一个元素 - webscraping selenium : my loop always get only the 1st element 使用Python仅收集网页中的href的第一级 - Collect only the 1st level of href in a webpage using Python python FOR循环在一个函数中连续2个循环只运行第一个循环 - python FOR loop 2 consecutive loops in one function only 1st loop runs 类型保护 function 第一个参数的属性类型由第二个字符串键参数确定(例如 key="Dict",然后 -&gt; TypeGuard["Dict"])? - a type guard function that 1st argument's attribute type is determined by the 2nd string key argument (e.g. key="Dict", then -> TypeGuard["Dict"])? 如何使用PYTHON访问保存为JSON中的键列表的所有词典的第一个键的值 - How to access the value of 1st key of all the dictionaries saved as a list for a key in JSON using PYTHON 带有 for 循环的美丽汤 find_all 仅返回第一个元素 - Beautiful Soup find_all with for loop only returning 1st element Python for 循环没有遍历所有元素? 它只取第一个元素 - Python for loop not looping through all the elements? It is only taking the 1st element 我怎么能像这样循环但得到一切而不是只有第一个 - how can i loop like this but getting everything instead of only the 1st one 对于第一种情况后的循环停止 - For loop stops after 1st case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM