I don't know why the value of 'add_command' variable in my code always be 'True'. Please help me. Here is my code:
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)
Please Note that: with line 3 & line 4 I can print 'Equal', it means add_commande should be change from True to False. But actulally with line 14: "print(add_command)", I saw that this varialble is always 'True'. Any help is highly appreciated!
**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
and here is Spec3 & Grade3 arrays:
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']
EDIT 2: I just delete else: add_command = True
and move add_command = False
to inside for i in range(0, len(Spec)):
loop then it works. Can't explain in logic way why it works. Here is revised code:
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)
The if condition was never true, so the add_command = False
was never reached. You could print the variables to see the run-time values, for example print('{}\\t{}\\t{}\\t{}'.format(row['Spec'], Spec3[i], row['Material'],Grade3[i])
before the IF statement.
If you know a set of values that should give you the true condition of IF, you can also check them through dataframe operations.
Ok here is your answer
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.