【问题标题】:Is there option to split excel file into slices based on columns?是否可以根据列将 excel 文件拆分为切片?
【发布时间】:2020-09-15 10:39:29
【问题描述】:

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

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

有人可以帮我怎么做吗?

【问题讨论】:

    标签: python excel pandas split


    【解决方案1】:

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

    low_memory=假

    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 有它,如果 int8 有它 int 8 并且如果 int64 有它相同的方式)

    编辑:在 read_excel 的情况下指定 Unicode

    【讨论】:

    • 虽然是excel文件,read_excel不支持low_memory
    • 是的,它的excel文件xlsx,如果我能打开它,我知道怎么玩,但我实际上无法读取文件
    • 你能试试“dtype”方法并将其设置为 Unicode 吗?
    【解决方案2】:

    查看read_excel中的usecols参数

    【讨论】:

    • 当然。参数是一样的。
    猜你喜欢
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 2012-09-24
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多