I'm not sure if this is going to help you exactly, but maybe it'll get you in the right direction. You can use Pandas.DataFrame.itertuples() to run across all rows in your dataframe, picking off values as you need them.
I went a bit further and created a quick column label dictionary to help sync the nested loops.
I tried to comment where necessary, but if you don't understand something, let me know!
import pandas as pd
import openpyxl
wb = load_workbook(filename='./generic_workbook_name.xlsx')
# Created smoe data for a dataframe.
fake_data_dict = {
'OpNo':['1','2','3','4',],
'StationNo':['11','22','33','44',],
'Spindle':['S1','S2','S3','S4',],
'OpDescription':['This','is','a','description',]
}
# Create the dataframe.
data = pd.DataFrame(fake_data_dict)
Our dataframe:
OpNo StationNo Spindle OpDescription
0 1 11 S1 This
1 2 22 S2 is
2 3 33 S3 a
3 4 44 S4 description
The rest of the script:
col_list = ['OpNo','StationNo','Spindle','OpDescription']
# Create a column label dictionary; Add 1 to index for Excel cells
col_dict = {i+1:v for i, v in enumerate(col_list)}
# Iterate over each row
for idx, row in enumerate(data.itertuples(), start = 1):
# For each key in our column dictionary [0, 1, 2, 3]
for key in col_dict.keys():
print('Row: {a}\n\tColumn: {b}\n\t\tValue: {c}'.format(a = idx, b = key,
# Reduce the index by 1; Get column name based on key value.
c = data.loc[idx - 1, col_dict[key]]))
Output:
Row: 1
Column: 1
Value: 1
Row: 1
Column: 2
Value: 11
Row: 1
Column: 3
Value: S1
Row: 1
Column: 4
Value: This
Row: 2
Column: 1
Value: 2
Row: 2
Column: 2
Value: 22
Row: 2
Column: 3
Value: S2
Row: 2
Column: 4
Value: is
Row: 3
Column: 1
Value: 3
Row: 3
Column: 2
Value: 33
Row: 3
Column: 3
Value: S3
Row: 3
Column: 4
Value: a
Row: 4
Column: 1
Value: 4
Row: 4
Column: 2
Value: 44
Row: 4
Column: 3
Value: S4
Row: 4
Column: 4
Value: description
With that in mind, this could simplify your script:
for idx, row in enumerate(data.itertuples(), start = 1):
for key in col_dict.keys():
wb['OneOpSheet'].cell(row = (idx + 11), column = (key + 1)).value = data.loc[idx - 1, col_dict[key]]
operationData.dtypesfor the info listing. Alsorow_indexer = (row_indexer + 1)and similar calls can be replaced byrow_indexer += 1.operationData.dtypes, every column is an object. And thanks for the+=tip!