[英]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
设置为weekday
和season
列:
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.