我不知道为什么我的代码中'add\u command'变量的值总是'True'。 请帮帮我。这是我的密码:
df3 = xls.parse(1)
# Function finding will return 4 arrays
Spec3, Grade3, other_scrap3, months3 = finding(df3)
# if Spec3[4] == df1.loc[1, 'Spec'] and Grade3[4] == df1.loc[1, 'Material']:
# print('Equal')
add_command = False # Variable to confirm need to add row for df1 or not
# Looping all row of data frame df1 then find out whether Spec3[i] & Grade3[i] in df1 or not.
for i in range(0, len(Spec3)):
date, mon = split_month(months3[i])
for ind, row in df1.iterrows():
if row['Spec'] == Spec3[i] and row['Material'] == Grade3[i]:
df1.loc[ind, mon] = other_scrap3[i]
add_command = False
else:
add_command = True
print(add_command)
if add_command == True:
df2 = pd.DataFrame(columns = column_names)
df2.loc[0, 'Spec'] = Spec3[i]
df2.loc[0, 'Material'] = Grade3[i]
df2.loc[0, mon] = other_scrap3[i]
df1 = df1.append(df2, ignore_index=True)
print(df1)
请注意:对于第3行和第4行,我可以打印'Equal',这意味着add_command应该从True更改为False。 但实际上,在第14行“print(add\u命令)”中,我看到这个变量总是“True”。 非常感谢您的帮助!你知道吗
**EDIT 1:**
Here is an example of df1:
Spec Material Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
1 40x25x2.0 SPHT-4 NaN NaN NaN NaN NaN NaN NaN NaN 52 NaN NaN
2 42.7x3.5 SPHT-1 NaN NaN NaN NaN NaN NaN NaN NaN 8 NaN NaN
3 42.7x1.6 SPHT-3 NaN NaN NaN NaN NaN NaN NaN NaN 8 NaN NaN
4 42.7x2.0 SPHT-4 NaN NaN NaN NaN NaN NaN NaN NaN 4 NaN NaN
5 22.2x3.2 SPHT-1 NaN NaN NaN NaN NaN NaN NaN NaN 20 NaN NaN
6 48.6x3.5 SPHT-1 NaN NaN NaN NaN NaN NaN NaN NaN 8 NaN NaN
7 42.0x1.0 HFS436L NaN NaN NaN NaN NaN NaN NaN NaN 14 NaN NaN
以下是Spec3和Grade3阵列:
Spec3:
['60x47.6x2.0', '60x47.6x2.0', '60.5x2.6', '60.5x2.0', '40x25x2.0', '25.4x3.2']
Grade3:
['SPHT2-D15M', 'SPHT-4', 'SPHT-3', 'SPHT-1', 'SPHT-4', 'SPHT2-D15M']
编辑2:
我只需删除else: add_command = True
并将add_command = False
移动到for i in range(0, len(Spec)):
循环内,它就可以工作了。
无法用逻辑的方式解释它为什么起作用。
修订代码如下:
df3 = xls.parse(1)
# Function finding will return 4 arrays
Spec3, Grade3, other_scrap3, months3 = finding(df3)
# if Spec3[4] == df1.loc[1, 'Spec'] and Grade3[4] == df1.loc[1, 'Material']:
# print('Equal')
#add_command = False **# Delete this line**
# Looping all row of data frame df1 then find out whether Spec3[i] & Grade3[i] in df1 or not.
for i in range(0, len(Spec3)):
add_command = True # Add this line
date, mon = split_month(months3[i])
for ind, row in df1.iterrows():
if row['Spec'] == Spec3[i] and row['Material'] == Grade3[i]:
df1.loc[ind, mon] = other_scrap3[i]
add_command = False
#else: **# Delete this line**
#add_command = True **# Delete this line**
print(add_command)
if add_command == True:
df2 = pd.DataFrame(columns = column_names)
df2.loc[0, 'Spec'] = Spec3[i]
df2.loc[0, 'Material'] = Grade3[i]
df2.loc[0, mon] = other_scrap3[i]
df1 = df1.append(df2, ignore_index=True)
print(df1)
好的,这是你的答案
if条件从未为true,因此从未达到
add_command = False
。可以打印变量以查看运行时值,例如IF语句前面的print('{}\t{}\t{}\t{}'.format(row['Spec'], Spec3[i], row['Material'],Grade3[i])
。你知道吗如果您知道一组值,这些值应该为您提供If的真实条件,那么您还可以通过dataframe操作来检查它们。你知道吗
相关问题 更多 >
编程相关推荐