Skip to content
TheChatArchiveThe Chat Archive
DeepSeekdeepseek-v3-0324· DeepSeekHugging Face LMArena Human Preference 140K

import pyarrow.dataset as ds dataset = ds.dataset(["file1.feather", "file2.feather"],…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with chatgpt-4o-latest-20250326’s answer.
  1. User

    import pyarrow.dataset as ds

    dataset = ds.dataset(["file1.feather", "file2.feather"], format="feather") result = dataset.to_table().to_pandas()

    我在用上述代码读取多个feather文件并合并时发现了一个问题,它只会保留在所有feather中都存在的列,但我想要的是保留所有出现过的列,如果在某个feather中该列没有存在则用nan填充,该怎么修改呢

  2. DeepSeek Assistant

    保留所有列合并多个Feather文件

    默认情况下,pyarrow.dataset的合并操作会执行列的交集操作(只保留所有文件共有的列)。要保留所有出现过的列(执行列的并集操作),你需要明确指定合并策略。

    解决方案

    你可以使用以下两种方法之一:

    方法1:使用unify_schemas=True参数

    import pyarrow.dataset as ds
    
    dataset = ds.dataset(["file1.feather", "file2.feather"], format="feather")
    result = dataset.to_table(combine_schemas=True).to_pandas()
    

    方法2:单独读取并合并(更灵活的方法)

    import pandas as pd
    import pyarrow.feather as feather
    
    files = ["file1.feather", "file2.feather"]
    dfs = [feather.read_feather(f) for f in files]
    result = pd.concat(dfs, ignore_index=True)
    

    说明

    1. 方法1:combine_schemas=True参数会先统一所有文件的schema(列结构),然后执行合并操作,缺失的列会自动填充为null。

    2. 方法2:使用pandas的concat函数,它会自动处理不同列的情况,缺失的列会被填充为NaN。这种方法更灵活,你可以控制更多的合并选项。

    如果文件很大,方法1(使用pyarrow.dataset)通常更高效,因为它是在Arrow层面进行合并。方法2则更灵活,适合需要更多控制的情况。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.