尝试连接来自 pandas 中 csv 的字段值时收到类型错误
Receiving type error when trying to concatenate field values from csv in pandas
我正在尝试使用从 .csv 中获取的字段生成完整的街角地址。我正在使用 pandas 并编写了以下代码来实现此结果。我收到了关于浮点对象的 TypeError,但数据框中的 none 字段是浮点数。我遍历了这些字段并打印了每一列类型,它们都返回了字符串……所以我有点困惑。
这是代码。如果这很简单,因为我还在学习,任何帮助将不胜感激和道歉:
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)
这是我收到的错误:TypeError: ("'float' object has no attribute 'getitem'", u'occurred at index 31549')
有人可以帮忙吗?
将文件类型从 .csv 转换为 .xlsx,函数 运行 很好,并允许我导出包含所需地址的 .xlsx。仍然不明白为什么 .csv 不起作用,但关闭它,因为我已经解决了我的问题。
我正在尝试使用从 .csv 中获取的字段生成完整的街角地址。我正在使用 pandas 并编写了以下代码来实现此结果。我收到了关于浮点对象的 TypeError,但数据框中的 none 字段是浮点数。我遍历了这些字段并打印了每一列类型,它们都返回了字符串……所以我有点困惑。
这是代码。如果这很简单,因为我还在学习,任何帮助将不胜感激和道歉:
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)
这是我收到的错误:TypeError: ("'float' object has no attribute 'getitem'", u'occurred at index 31549')
有人可以帮忙吗?
将文件类型从 .csv 转换为 .xlsx,函数 运行 很好,并允许我导出包含所需地址的 .xlsx。仍然不明白为什么 .csv 不起作用,但关闭它,因为我已经解决了我的问题。