简体   繁体   中英

Manipulating a pandas dataframe then saving the changes

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

I ran your code and got this error, so the DataFrame wasn't being modified:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

So to match the format of .loc[row_indexer, col_indexer], I changed the line

df.loc[df['Voucher Code'] == voucher_code, 'Used'] = 'Yes'

PS You can generate a random integer up to the number of rows by using df.shape

index = random.randint(0, df.shape[0])

Hope this helped

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM