Reputation: 5914
I have a loop that is setting the values of one list to the value of another and while I am able to achieve this with my current code it is only appending the last grouping of values to my list. What could possibly be off in my setup that could cause this behavior?
Here is my data format:
print(df_full_raw_format) # Data Set being set to worksheet_range values
<class 'pandas.core.frame.DataFrame'>
b_clicks b_cpc date_string
0 b_clicks b_cpc date_string
1 72 2.43 2018-01-01
2 232 2.8 2018-01-02
3 255 2.6 2018-01-03
4 249 2.65 2018-01-04
5 202 2.86 2018-01-05
Here is my worksheet_range
format:
[<Cell R1C1 ''>, <Cell R2C1 ''>, <Cell R3C1 ''>,....]
Here is my function with the for loops:
update_raw_data_sheet(df_full_raw_format)
def update_raw_data_sheet(data_set):
for col_val, col_name in zip(gs_columns, columns):
updated_values = [] # List storing the combined list values
column_data = data_set[col_name].values.tolist(); # col_name = ['b_clicks', 'b_cpc', 'date_string']
print("COLUMN DATA")
print(column_data)
worksheet_range = worksheet.range(1, col_val, len(column_data), col_val); # [row_start, col_start, row_end, col_end]
print(worksheet_range)
for cell, data in zip(worksheet_range, column_data):
cell.value = data # <Cell R1C1 '2018-01-01'>...
print(cell)
updated_values.append(cell)
print(updated_values)
Here is the console:
(Loop 1)
COLUMN DATA
['date_string', '2018-01-01', '2018-01-02', '2018-01-03', ...] # print(column_data)
[<Cell R1C1 ''>, <Cell R2C1 ''>, <Cell R3C1 ''>,...] # print(worksheet_range)
<Cell R1C1 'date_string'> # print(cell)
<Cell R2C1 '2018-01-01'>
<Cell R3C1 '2018-01-02'>
<Cell R4C1 '2018-01-03'>
... (Loop 2)
COLUMN DATA
['b_clicks', '72', '232', '255', ...]
[<Cell R1C2 ''>, <Cell R2C2 ''>, <Cell R3C2 '', ...]
<Cell R1C2 'b_clicks'>
<Cell R2C2 '72'>
<Cell R3C2 '232'>
... (Loop 3)
COLUMN DATA
['b_cpc', 2.43, 2.8,...]
[<Cell R1C3 'b_cpc'>, <Cell R2C3 '2.43'>, <Cell R3C3 '2.8'>, ...]
<Cell R1C3 'b_cpc'>
<Cell R2C3 2.43>
<Cell R3C3 2.8>
(Post-Loop)
[<Cell R1C3 'b_cpc'>, <Cell R2C3 2.43>, <Cell R3C3 2.8>, ...] # print(updated_values)
It misses the first two loops worth of values, but perfectly captures the 3rd in the array.
Upvotes: 2
Views: 8199
Reputation: 77837
This is exactly what you programmed. You reset updated_values
on each iteration of the outer loop, which wipes out the previous values:
for col_val, col_name in zip(gs_columns, columns):
updated_values = [] # List storing the combined list values
If you want all three iterations` of data, then you need to lift the initialization out of the loop:
updated_values = [] # List storing the combined list values
for col_val, col_name in zip(gs_columns, columns):
Upvotes: 0
Reputation: 26039
updated_values = []
should be outside loop just after def
statement, otherwise you overwrite values added to the list in every iteration.
def update_raw_data_sheet(data_set):
updated_values = []
# ...
Upvotes: 4