简体   繁体   English

如何将“INVALID”分配给 function 的所有输出?

[英]How to assign “INVALID” to all outputs of function?

I have to define a function that takes inputs Year, Crash_Month, Crash_Day and Crash_Time and returns the outputs season(Summer for months 12, 1, 2, Autumn for months 3,4,5 etc) weekday (Monday:1 to Sunday:7) and time_of_day (as Morning, Afternoon, Evening and Night).我必须定义一个 function 接受输入 Year、Crash_Month、Crash_Day 和 Crash_Time 并返回输出季节(夏季为 12、1、2 个月,秋季为 3、4、5 个月等)工作日(周一:1 至周日:7 ) 和 time_of_day (如早上、下午、晚上和晚上)。 I have created the following code to define my function:我创建了以下代码来定义我的 function:

df = pd.DataFrame({'year': (data_dict['Year']),
            'season': (data_dict['Crash_Month']),
            'weekday': (data_dict['Crash_Day']),
            'time_of_day': (data_dict['Crash_Time'])})
df['time_of_day'] = pd.to_datetime(df['time_of_day'], format = "%H:%M:%S", errors = 'coerce')
d = {1:'Monday', 2:'Tuesday', 3:'Wednesday', 4:'Thursday', 5:'Friday', 6:'Saturday', 7:'Sunday'}
df['weekday'] = df['weekday'].map(d)
s = {12:'Summer', 1:'Summer', 2:'Summer', 3:'Autumn', 4:'Autumn', 5:'Autumn', 6:'Winter', 7:'Winter', 8: 'Winter', 9: 'Spring', 10: 'Spring', 11:'Spring'}
df['season'] = df['season'].map(s)
df['time_of_day']=df['time_of_day'].dt.hour.apply(lambda x: np.select([0<=x<6,
                                                                       6<=x<12,
                                                                       12<=x<18,
                                                                       12<=x<24],
                                                                      ['Night', 'Morning', 'Afternoon', 'Evening']))    
def compute_time_day_year(df):
    return(df)

It returns a data frame with the required outputs.它返回具有所需输出的数据帧。 However, in the excel file some of the Crash_Time values are "UNKNOWN".但是,在 excel 文件中,一些 Crash_Time 值是“未知”。 For these "UNKNOWN" entries I want to assign all outputs in the data frame (season, weekday, time) as 'INVALID". I have tried to use:对于这些“未知”条目,我想将数据框中的所有输出(季节、工作日、时间)分配为“无效”。我尝试使用:

while True:
    try:
        compute_time_day_year(df)
    except ValueError:
        return 'INVALID' 

but the output does not change any values to INVALID.但 output 不会将任何值更改为无效。 Does anyone have any advice on how to assign all output to 'INVALID'?有人对如何将所有 output 分配给“无效”有任何建议吗?

Because to_datetime has parameter errors = 'coerce' it return missing values for UNKNOWN strings, so then set NaN s to weekday and season columns:因为to_datetime有参数errors = 'coerce'它返回UNKNOWN字符串的缺失值,所以然后将NaN设置为weekdayseason列:

df['time_of_day'] = pd.to_datetime(df['time_of_day'], format = "%H:%M:%S", errors = 'coerce')
df.loc[df['time_of_day'].isna(), ['weekday','season']] = np.nan

If want set all columns:如果要设置所有列:

df.loc[df['time_of_day'].isna()] = np.nan
#or
#df = df.mask(df['time_of_day'].isna())

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

相关问题 如何从 keras 中的单个自定义损失 function 访问所有输出 - How to access all outputs from a single custom loss function in keras function 的所有输出都相同 - All outputs from function are the same 如何将“描述”方法的输出分配给变量? - How to assign to a variable the outputs of the method “describe”? 使用 for 循环将 function 输出分配给列表中的变量 - Assign function outputs to variables in list using for-loop 使用嵌套的 for 循环将函数输出分配给嵌套的 dict - Using nested for loops to assign function outputs to a nested dict How to import a function from an R package as if it was native Python function and use all its outputs? - How to import a function from an R package as if it was native Python function and use all its outputs? 在添加模函数后,所有输出均为零 - After adding a modulo to function, all outputs are zero 如何存储可迭代 function 的输出 - How to storage the outputs of an iterable function 从一个函数返回所有可能的输出,然后在另一个函数中使用它 - return all possible outputs from a function then use it in another function 如何在Keras中为每个输出应用S型函数? - How to apply sigmoid function for each outputs in Keras?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM