我不知道为什么我的代码中'add_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行中,我可以打印“等于”,这意味着add_commande应该从True更改为False。 但实际上在第14行:“ print(add_command)”,我看到此变量始终为'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)
答案 0 :(得分:0)
if条件从不成立,因此从未达到add_command = False
。您可以打印变量以查看运行时值,例如IF语句之前的print('{}\t{}\t{}\t{}'.format(row['Spec'], Spec3[i], row['Material'],Grade3[i])
。
如果您知道一组应该为您提供IF真实条件的值,则还可以通过数据框操作对其进行检查。
答案 1 :(得分:0)
好的,这是你的答案
for i in range(0, len(Spec3)): #you loop for each index in spec3
add_command = True # Add this line
date, mon = split_month(months3[i])
for ind, row in df1.iterrows(): #then you loop through the rows
if row['Spec'] == Spec3[i] and row['Material'] == Grade3[i]:
df1.loc[ind, mon] = other_scrap3[i]
add_command = False
# Here it changes to False once it found the condition in the row that is why it is working
#else: **# Delete this line**
#add_command = True **if you have the add_command =True**
# For each item the add_command will be different it will change all the time
# so the add command will give the value for the last item all the time
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)