【发布时间】:2019-12-22 08:36:05
【问题描述】:
我正在尝试为我的数据框编写一个嵌套在 for 循环中的 if 语句,如下所示:
我希望代码遍历数据帧的每一行,如果它在 Detection_Location 列中检测到“CV22”,则应将一个文件作为数据帧导入,如果在 Detection_location 列中检测到“CV23”,则应导入另一个文件与之前相同的数据帧。
我已尝试为此编写以下代码:
def Get_PHD(df2):
if (df2['Detection_Location'] == 'CV22'):
PHD_df = pd.read_excel(r'C:\Users\s.gaur\Desktop\LS1 - Edited file.xlsx', sheet_by_name = "Sheet1")
return (PHD_df)
elif (df2['Detection_Location'] == 'CV23'):
PHD_df = pd.read_excel(r'C:\Users\s.gaur\Desktop\LS2 - Edited File.xlsx', sheet_by_name = "Sheet1")
return (PHD_df)
for index, row in df2.iterrows():
Get_PHD(df2)
但出现以下错误:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
谁能帮我解决我做错的事情。
【问题讨论】:
-
数据截图对复制没有太大帮助; Provide a copy of the DataFrame
-
@Trenton_M - 数据集实际上与图片中可见的一样小。实际上没有更多的东西。
标签: python dataframe for-loop if-statement