我使用for循环创建了一个函数,该函数通过查看每一行的方向值来根据条件返回方向。
我将整个数据集一分为二。
我在用:
def dir_func(df):
for i in df.index:
if df.loc[i,'dir_1'] == ['E' or 'NE' or 'SE']:
df.loc[i,'dir_fin'] = 'E'
elif df.loc[i,'dir_1'] == ['W' or 'NW' or 'SW']:
df.loc[i,'dir_fin'] = 'W'
else:
if df.loc[i,'dir_2'] == ['E' or 'NE' or 'SE']:
df.loc[i,'dir_fin'] = 'E'
elif df.loc[i,'dir_2'] == ['W' or 'NW' or 'SW']:
df.loc[i,'dir_fin'] = 'W'
else:
if (pd.Series(df.loc[i,'loc_01']).str.contains(
("\sEB\s" or "\sEast\s" or "\sEastbound" or "\sE\s"),case=False) \
| pd.Series(speed_overspeed.loc[i,'loc_02']).str.contains(
("\sEB\s" or "\sEast\s" or "\sEastbound" or "\sE\s"),case=False)).bool()==True:
df.loc[i,'dir_fin'] = 'E'
elif (pd.Series(df.loc[i,'loc_01']).str.contains(
("\sWB\s" or "\sWest\s" or "\sWestbound" or "\sW\s"),case=False) \
| pd.Series(speed_overspeed.loc[i,'loc_02']).str.contains(
("\sWB\s" or "\sWest\s" or "\sWestbound" or "\sW\s"),case=False)).bool()==True:
df.loc[i,'dir_fin'] = 'W'
else:
df.loc[i,'dir_fin'] = 'NA'
进行迭代。
此函数对我的第一个拆分数据集效果很好,但是当我尝试将其应用于第二个数据集时,它将返回:
KeyError:“标签[891]不在[index]中”
但是,当我查看#891值时,它实际上返回包含值的行。
我想知道是否有人可以帮助我解决这个问题,我将不胜感激。
非常感谢!
参考方案
我认为您应该了解(在python 3.x中)的结果
df.loc[i,'dir_1'] == ('E' or 'NE' or 'SE')
除非df.loc [i,'dir_1']实际上等于'E',否则总是False
因此,if
语句应更改为类似
if df.loc[i,'dir_1'] in ['E', 'NE', 'SE']:
同样,您可以在最后一个比较语句中使用in
。
最终功能将是:
def dir_func(df):
for i in df.index:
if df.loc[i,'dir_1'] in ['E', 'NE', 'SE']:
df.loc[i,'dir_fin'] = 'E'
elif df.loc[i,'dir_1'] in ['W', 'NW', 'SW']:
df.loc[i,'dir_fin'] = 'W'
else:
if df.loc[i,'dir_2'] in ['E', 'NE', 'SE']:
df.loc[i,'dir_fin'] = 'E'
elif df.loc[i,'dir_2'] in ['W', 'NW', 'SW']:
df.loc[i,'dir_fin'] = 'W'
else:
if (df.loc[i,'loc_01'].lower() in ("\seb\s", "\seast\s", "\seastbound", "\se\s")) or (speed_overspeed.loc[i,'loc_02'] in ("\seb\s", "\seast\s", "\seastbound", "\se\s")):
df.loc[i,'dir_fin'] = 'E'
elif (df.loc[i,'loc_01'].lower() in ("\swb\s", "\swest\s", "\swestbound", "\sw\s")) or (speed_overspeed.loc[i,'loc_02'] in ("\swb\s", "\swest\s", "\swestbound", "\sw\s")):
df.loc[i,'dir_fin'] = 'W'
else:
df.loc[i,'dir_fin'] = 'NA'
在返回'Response'(Python)中传递多个参数 - python我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…
您如何在列表内部调用一个字符串位置? - python我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
Python exchangelib在子文件夹中读取邮件 - python我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…
R'relaimpo'软件包的Python端口 - python我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。