简体   繁体   English

从循环内将数据框列转换为整数

[英]Convert dataframe column into integers from within loop

I'm trying loop through a folder of csv's and put them into a dataframe, change certain columns into an integer, before passing them through a Django model. 我正在尝试遍历csv的文件夹并将其放入数据框,将某些列更改为整数,然后再将它们传递给Django模型。 Here is my code: 这是我的代码:

import glob
import pandas as pd   
path = 'DIV1FCS_2017/*/*'

for fname in glob.glob(path):
    df = pd.read_csv(fname)
    df['Number'].apply(pd.to_numeric)

I am receiving the following: ValueError: Unable to parse string 我收到以下信息:ValueError:无法解析字符串

Does anybody know if I can convert a column of strings into integers using pd.to_numeric from within a loop? 有人知道我是否可以在循环中使用pd.to_numeric将一列字符串转换为整数吗? Outside of the loop it seems to work properly. 在循环之外,它似乎可以正常工作。

I think you probably have some non-numbers data stored in your dataframe, and that's what's casuing the error. 我认为您可能在数据框中存储了一些非数字数据,这就是导致错误的原因。 You can examine your data and make sure everything's fine. 您可以检查数据并确保一切正常。 In the meantime, you can also do pd.to_numeric(errors="ignore") to ignore errors for now. 同时,您现在也可以执行pd.to_numeric(errors =“ ignore”)来忽略错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM