我定義了一個如下所示的函數:def incident_rate(substation,year,events): age = conductor_yearly_df.loc[conductor_yearly_df['SUBSTATION']==substation,conductor_yearly_df.columns.str.contains(year)].reset_index(drop=True).values[0][0] length = conductor_yearly_df.loc[conductor_yearly_df['SUBSTATION']==substation,conductor_yearly_df.columns.str.contains(year)].reset_index(drop=True).values[0][1] temp = events / (age * length) return temp當我在代碼中調用該函數時(例如:incident_rate('WB',2019,6),它會拋出以下錯誤:TypeError: first argument must be string or compiled pattern我不確定我在哪里犯了錯誤。誰能指導我如何解決錯誤?
1 回答

尚方寶劍之說
TA貢獻1788條經驗 獲得超4個贊
將年份更改為字符串str(year)
,因為str.contains
接受字符串類型
age = conductor_yearly_df.loc[conductor_yearly_df['SUBSTATION']==substation,conductor_yearly_df.columns.str.contains(str(year))].reset_index(drop=True).values[0][0]
添加回答
舉報
0/150
提交
取消