除了 pandas 和 dask 之外,还有什么更快的读取大数据集和应用行式操作的方法?
What are faster ways of reading big data set and apply row wise operations other than pandas and dask?
我正在编写一个代码,我需要根据大 table 的每一行填充一组数据结构。现在,我正在使用 pandas 读取数据并进行一些基本的数据验证预处理。但是,当我进入流程的其余部分并将数据放入相应的数据结构时,需要相当长的时间才能完成循环并填充我的数据结构。例如,在下面的代码中,我有一个包含 15 M 条记录的 table。 Table 有三列,我根据每一行创建一个 foo() 对象并将其添加到列表中。
# Profile.csv
# Index | Name | Family| DB
# ---------|------|-------|----------
# 0. | Jane | Doe | 08/23/1977
# ...
# 15000000 | Jhon | Doe | 01/01/2000
class foo():
def __init__(self, name, last, bd):
self.name = name
self.last = last
self.bd = bd
def populate(row, my_list):
my_list.append(foo(*row))
# reading the csv file and formatting the date column
df = pd.read_csv('Profile.csv')
df['DB'] = pd.to_datetime(df['DB'],'%Y-%m-%d')
# using apply to create an foo() object and add it to the list
my_list = []
gf.apply(populate, axis=1, args=(my_list,))
所以在使用 pandas 将字符串日期转换为日期对象之后,我只需要遍历 DataFrame 来创建我的对象并将它们添加到列表中。这个过程非常耗时(在我的真实示例中,它甚至花费了更多时间,因为我的数据结构更复杂并且我有更多的列)。所以,我想知道在这种情况下增加我的 运行 时间的最佳做法是什么。我什至应该使用 pandas
来阅读我的大 table 并逐行处理它们吗?
使用文件句柄会更快:
input_file = "profile.csv"
sep=";"
my_list = []
with open(input_file) as fh:
cols = {}
for i, col in enumerate(fh.readline().strip().split(sep)):
cols[col] = i
for line in fh:
line = line.strip().split(sep)
date = line[cols["DB"]].split("/")
date = [date[2], date[0], date[1]]
line[cols["DB"]] = "-".join(date)
populate(line, my_list)
对于这种情况有多种方法,但是,最快和最有效的方法是尽可能使用矢量化。我在此 post 中使用矢量化演示的示例的解决方案如下:
my_list = [foo(*args) for args in zip(df["Name"],df["Family"],df["BD"])]
如果无法进行矢量化,将数据帧转换为字典可以显着提高性能。对于当前示例,if 将类似于:
my_list = []
dc = df.to_dict()
for i, j in dc.items():
my_list.append(foo(dc["Name"][i], dc["Family"][i], dc["BD"][i]))
如果结构和流程的类型更复杂,最后一个解决方案特别有效。
我正在编写一个代码,我需要根据大 table 的每一行填充一组数据结构。现在,我正在使用 pandas 读取数据并进行一些基本的数据验证预处理。但是,当我进入流程的其余部分并将数据放入相应的数据结构时,需要相当长的时间才能完成循环并填充我的数据结构。例如,在下面的代码中,我有一个包含 15 M 条记录的 table。 Table 有三列,我根据每一行创建一个 foo() 对象并将其添加到列表中。
# Profile.csv
# Index | Name | Family| DB
# ---------|------|-------|----------
# 0. | Jane | Doe | 08/23/1977
# ...
# 15000000 | Jhon | Doe | 01/01/2000
class foo():
def __init__(self, name, last, bd):
self.name = name
self.last = last
self.bd = bd
def populate(row, my_list):
my_list.append(foo(*row))
# reading the csv file and formatting the date column
df = pd.read_csv('Profile.csv')
df['DB'] = pd.to_datetime(df['DB'],'%Y-%m-%d')
# using apply to create an foo() object and add it to the list
my_list = []
gf.apply(populate, axis=1, args=(my_list,))
所以在使用 pandas 将字符串日期转换为日期对象之后,我只需要遍历 DataFrame 来创建我的对象并将它们添加到列表中。这个过程非常耗时(在我的真实示例中,它甚至花费了更多时间,因为我的数据结构更复杂并且我有更多的列)。所以,我想知道在这种情况下增加我的 运行 时间的最佳做法是什么。我什至应该使用 pandas
来阅读我的大 table 并逐行处理它们吗?
使用文件句柄会更快:
input_file = "profile.csv"
sep=";"
my_list = []
with open(input_file) as fh:
cols = {}
for i, col in enumerate(fh.readline().strip().split(sep)):
cols[col] = i
for line in fh:
line = line.strip().split(sep)
date = line[cols["DB"]].split("/")
date = [date[2], date[0], date[1]]
line[cols["DB"]] = "-".join(date)
populate(line, my_list)
对于这种情况有多种方法,但是,最快和最有效的方法是尽可能使用矢量化。我在此 post 中使用矢量化演示的示例的解决方案如下:
my_list = [foo(*args) for args in zip(df["Name"],df["Family"],df["BD"])]
如果无法进行矢量化,将数据帧转换为字典可以显着提高性能。对于当前示例,if 将类似于:
my_list = []
dc = df.to_dict()
for i, j in dc.items():
my_list.append(foo(dc["Name"][i], dc["Family"][i], dc["BD"][i]))
如果结构和流程的类型更复杂,最后一个解决方案特别有效。