I'm attempting to manipulate a pandas dataframe to select a random index from the output of a random integer generator, then take that value to be used, and set the 'Used' column to yes, the save the csv again.
The code I have is the following:
import random
import pandas
# Read in the dataframe:
df = pandas.read_csv('./data/voucher_codes.csv', encoding='utf-8')
# Generate a random integer (this would be nice if the max value was the number of rows - but I'll figure this out later!)
index = random.randint(0, 3)
# Store this code in memory selecting only when Used = 'No':
voucher_code = df.loc[df['Used'] == 'No'].iloc[[index]]['Voucher Code'].values[0]
# Update the column associated to the above voucher code to "Yes'
df.loc[df['Voucher Code'] == voucher_code]['Used'] = 'Yes'
# Save said dataframe, to be consistent with what voucher codes have been used:
df.to_csv('./data/voucher_codes.csv', sep=',')
.csv isn't be overwritten though!
Sample data might be useful:
000001,No
000002,No
000003,No
000004,No
This is the dataframe from @ALollz's suggestion:
,Unnamed: 0,Voucher Code,Used
0,0,000001,No
1,1,000002,No
2,2,000003,No
3,3,000004,No