Python Forum

Full Version: Pandas/Excel, reading from one column writing back to another...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys

Can you help me process the data in a spreadsheet, in particular to read values in one column apply some functions and then write the results back to another column.

I will simplify my problem as much as I can. The example I am going to give can be easily done in Excel without any 3rd party help, I just want to make the code as easy to read as possible, so that I can apply it back to my own case - which is a bit more complicated, with additional functions inside the main 'for' loop. All the code there is working fine though.
import 


import pandas as pd
df = pd.read_excel('serials.xlsx', sheet_name='Sheet1')
listSerialsLower = df['Serial_Lower']
listSerialsUpper = df['Serial_Upper']

writer = pd.ExcelWriter('serials.xlsx', engine='xlsxwriter')

for i in listSerialsLower:
    inputSerial = i
    upperSerial = str(inputSerial).upper()

    #please let me know how I can insert the corresponding values
    #for the lowercase serials into the list (or series?) for the uppercase serials
    #so that they would appear in the same row in the excel document. 

    #i have tried without success...
    listSerialsUpper[i] = upperSerial

df.to_excel(writer)
writer.save()


Thanks for helping me out.

WL
I've solved it myself. Just really need a while loop instead of a for loop and to make one new variable, the size of the list.

sizeOfSerialsList = len(listSerials)
count = 0

while (count < sizeOfSerialsList):
    inputSerial = listSerials.iloc[count]
    inputSerial = str(inputSerial).upper()
    modelCodeIsolatedFromSerial = ""
    model = ""


    if len(inputSerial) == 12:
        modelCodeIsolatedFromSerial = inputSerial[-4:]
    elif len(inputSerial) == 11:
        modelCodeIsolatedFromSerial = inputSerial[-3:]

    try:
        model = databaseOfMacs[modelCodeIsolatedFromSerial]
        listModels.iloc[count] = model
    except:
        listModels.iloc[count] = "Not found"

    count = count + 1