0

I am attempting to generate full street corner addresses out of fields taken from a .csv. I am using pandas and have written the following code to achieve this result. I am getting back a TypeError about a float object but none of the fields in my dataframe are floats. I iterated through the fields and printed each column type and they all come back strings.. so I am a bit confused.

Here is the code. Any help would be greatly appreciated and apologies if this is something simple as I am still learning:

import pandas as pd
df_CaveIns = pd.read_csv("DEP_Cave_in_CARs.csv")
df_CaveIns['Address'] = ''

df_CaveIns['Address'].to_string()

boro_key = {
'Q' : 'QUEENS',
'S' : 'STATEN ISLAND',
'M' : 'MANHATTAN',
'B' : 'BROOKLYN',
'X' : 'BRONX'
}

def createAddress(row):
    if pd.isnull(row['From_Street_Name']) == False and pd.isnull(row['To_Street_Name']) == False:
        boro_code = row['Permit'][0]    
        if boro_code in boro_key:address = "%s AND %s, %s, NEW YORK" %(str(row['On_Street_Name']).strip(),str(row['To_Street_Name']).strip(),str(boro_key[boro_code]))
            #print address
            return address
    elif pd.isnull(row['From_Street_Name']):
        boro_code = row['Permit'][0]    
        if boro_code in boro_key:address = "%s AND %s, %s, NEW YORK" %(str(row['On_Street_Name']).strip(),str(row['To_Street_Name']).strip(),str(boro_key[boro_code]))
            #print address
            return address
    elif pd.isnull(row['To_Street_Name']):
        boro_code = row['Permit'][0]    
        if boro_code in boro_key:address = "%s AND %s, %s, NEW YORK" %(str(row['On_Street_Name']).strip(),str(row['From_Street_Name']).strip(),str(boro_key[boro_code]))
            #print address
            return address
    else:
        return None

df_CaveIns['Address'] = df_CaveIns.apply(createAddress, axis=1)            

This is the error I'm getting: TypeError: ("'float' object has no attribute 'getitem'", u'occurred at index 31549')

Can anyone help with this?

MapPeddler
  • 53
  • 9
  • `str(boro_code) in ...`? – Alexander Sep 22 '17 at 20:02
  • Where do you see that piece of code? I stopped trying to convert my dictionary value to a string and am still receiving the error: str(boro_key[boro_code]) became (boro_key[boro_code]) but when I run I still get the float object has no attribute __getitem__ error. It's weird because when I comment out the returns and just print the addresses they print fine until hitting the error – MapPeddler Sep 26 '17 at 13:34

1 Answers1

0

Converted file type from .csv to .xlsx and the function ran just fine and allowed me to export an .xlsx with the needed addresses. Still don't understand why the .csv didn't work, but closing this as I have solved my problem.

MapPeddler
  • 53
  • 9