mardi 31 juillet 2018

How to iterate through pandas columns and replace cells with information with the next row down

I am trying to loop through a pandas dataframe column and based on if the next row down does not include "Property Address" add the information from that next row down to the previous row. For example, if I have a column that goes from top to bottom ["Property Address", "Alternate Address", "Property Address"] I would like to take the information from "Alternate Address" and add that information to the column above it ("Property Address"). I have already double checked that there are no trailing or leading spaces and that everything is lower case so that all comparisons will work. However, I still get this error:

if i == "Property Address" and df.loc[i+1, :] != "Property Address":

TypeError: must be str, not int

Does anyone have ideas on what I can do so that this will work? I am new to Python, and I am really lost. Please let me know if there is any more information that I should provide to make answering this question easier. Thanks

Here is my code so far:

import pandas as pd
import time

df = pd.read_excel('BRH.xls') # Reads the Excel File and creates a 
dataframe 
# Column Headers
df = df[['street', 'state', 'zip', 'Address Type', 'mStreet', 'mState', 'mZip']]

propertyAddress = "Property Address" # iterates thru column and replaces 
the current row with info from next row down

for i in df['Address Type']:
  if i == "Property Address" and df.loc[i+1, :] != "Property Address":
      df['mStreet'] == df.loc[i + 1, 'street']
      df['mState'] == df.loc[i + 1, 'state']
      df['mZip'] = df.loc[i + 1, 'zip']

df.to_excel('BRHOut.xls')
print('operation complete in:', time.process_time(), 'ms')

Aucun commentaire:

Enregistrer un commentaire