[英]Very Big CSV File - How to Read Only Certain Rows into Data Frame
我有一個非常大的 csv 文件,我無法使用 pandas read_csv 加載到我的計算機 memory。
我看dask.dataframe as dd
我需要使用 dask 從該 csv 文件中僅讀取某些列的某些行並將其存儲為熊貓 dataframe。
例如:
User ProductA ProductB
A 1 2
B 2 3
C 3 1
如何使用 dask 僅讀取用戶 C 的行和 ProductA 列?
需要 output 作為數據幀:
User ProductA
C 3
You can use the read_csv
function of dask.dataframe
, filter and then transform your df
to a pandas dataframe:
import dask.dataframe as dd
import pandas as pd
path2file = "yourpath.csv"
cols = ["User", "ProductA"]
# Be careful about the sep (check if it is ; or something else and add it to the
# function below as parameter if so
dataset = dd.read_csv(path2file, usecols=cols)
# Filter
dataset = dataset.loc[dataset["User"]=="C"]), :]
dataset = dataset.compute()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.