jpp*_*jpp 5 python dataframe pandas dask
我经常dask.dataframe
用来读取多个文件,如下所示:
import dask.dataframe as dd
df = dd.read_csv('*.csv')
Run Code Online (Sandbox Code Playgroud)
然而,每一行的来源,即数据是从哪个文件读取的,似乎永远丢失了。
有没有办法将它添加为一列,例如,df.loc[:100, 'partition'] = 'file1.csv'
如果file1.csv
是第一个文件并包含 100 行。当compute
作为工作流的一部分触发时,这将应用于读入数据帧的每个“分区”/文件。
这个想法是然后可以根据来源应用不同的逻辑。
Dask 函数read_csv、read_table和read_fwf现在包含一个参数include_path_column
:
include_path_column:bool or str, optional
Whether or not to include the path to each particular file.
If True a new column is added to the dataframe called path.
If str, sets new column name. Default is False.
Run Code Online (Sandbox Code Playgroud)