Python 数据帧中的数据类型转换问题

Python 数据帧中的数据类型转换问题,python,pandas,Python,Pandas,我有一个csv文件,看起来像是从一个!猫 ,City,region,Res_Comm,mkt_type,Quradate,National_exp,Alabama_exp,Sales_exp,Inventory_exp,Price_exp,Credit_exp 0,Dothan,South_Central-Montgomery-Auburn-Wiregrass-Dothan,Residential,Rural,2010-01-15,2,2,3,2,3,3 1,Dothan,South_Centr

我有一个csv文件,看起来像是从一个!猫

,City,region,Res_Comm,mkt_type,Quradate,National_exp,Alabama_exp,Sales_exp,Inventory_exp,Price_exp,Credit_exp
0,Dothan,South_Central-Montgomery-Auburn-Wiregrass-Dothan,Residential,Rural,2010-01-15,2,2,3,2,3,3
1,Dothan,South_Central-Montgomery-Auburn-Wiregrass-Dothan,Residential,Suburban_Urban,2010-07-15,2,2,3,2,2,2
2,Dothan,South_Central-Montgomery-Auburn-Wiregrass-Dothan,Residential,Suburban_Urban,2011-01-15,2,2,2,2,2,2
当我通过read_csv读取它时,我会得到一个数据帧,所有的…\u exp字段都是一位数,我需要用它做基本的数学运算(当我使用read table和另一个文件变体时,它工作得很好)

但当我做任何数学运算时,我会得到一个类型错误,指示列是字符串,例如:

df['Credit_exp'] = df['Credit_exp']/2
TypeError: unsupported operand type(s) for /: 'str' and 'int'
我不知道如何将其转换为int? 我尝试在文件读取选项中指定字段类型,如dtype={'Credit_exp':np.int32,…,但不喜欢这样 我试着做一个类型转换,比如 df['Credit\u exp']=int(df['Credit\u exp']) 这给了我:

TypeError: only length-1 arrays can be converted to Python scalars
因此,我显然遗漏了一些东西…

请尝试以下方法:

df.Credit_exp.astype('int')

你使用哪个版本的pandas?你的示例对我来说是0.12。否则
df['Credit\u exp']。apply(int)
可以完成这个任务。注意:你的分区将是euclideanOn 12+dev of pandas..所以我会尝试apply(int)我仍然不明白为什么Dtype={对read\u csv不起作用?看起来你的原始数据不干净。“Credit\u exp”列可能包含一些字符串值。请尝试data['Credit\u exp'].astype('int')并查看收到的错误消息。df['Credit\u exp']=df['Credit\u exp']。apply(int)为我提供ValueError:int()的文本无效以10为基数:'\\N'@dartdog:这意味着您的
Credit\u exp
值之一不是一个数字,它看起来像是一个损坏的端点标记。df['Credit\u exp']=df.Credit\u exp.astype('int')给我:::ValueError:long无效文本()以10为基数:'\\N'这是因为列中的数据不是异常所指示的整数。这可能是文件格式化的原因
df.Credit_exp.astype('int')