是否有基于列将excel文件拆分为切片的选项?

2024-09-27 04:24:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有5000行的excel文件,每行有17000列,可以选择使用python/pandas拆分此文件,因为现在当我尝试读取excel时,它返回MemoryError 如果我能以某种方式读取文件,我可以减少列数

drop(list(myFile.filter(regex=r'(x|y)')))

有人能帮我怎么做吗


Tags: 文件pandas方式filtermyfileexcellistregex
2条回答

在pandas中,您需要设置参数,并且应该为csv列指定数据类型。 比如说

low_memory= False

df = pd.read_csv("YOURFILENAME.csv", delimiter = '|',error_bad_lines=False, 
                 index_col=False, 
                 dtype='unicode') # , # This or the other one
                 #dtype={"user_id": int, "username": "string"}, low_memory = False)

最好的做法是为各个列指定数据类型,以防因为案例中有大量列而无法指定。您只需使用Try,第二列除外,并遍历值(如果string有string,如果int8有int 8,如果int64有相同的方式)

编辑:在读取excel时指定Unicode

查看read_excel中的usecols参数

相关问题 更多 >

    热门问题