1.pd.read_csv()
这行代码使用了 Pandas 库中的 read_csv()
函数来读取名为 "tweets.csv" 的 CSV 文件,并且设置了 chunksize
参数为 10。这意味着该 CSV 文件将被分块读取,每个块的大小为 10 行数据。
在循环中,每次迭代都会从文件中读取一个块(包含10行数据),然后将这个块赋值给变量 chunk
。在每次迭代中,你可以对变量 chunk
进行操作,处理该块的数据,例如进行数据分析、转换、过滤等操作。
这种分块读取的方式特别适用于处理大型数据集,因为它可以有效地节省内存,避免一次性加载整个数据集到内存中。
示例:
# Initialize an empty dictionary: counts_dict
counts_dict={}
# Iterate over the file chunk by chunk
for chunk in pd.read_csv("tweets.csv",chunksize=10):
# Iterate over the column in DataFrame
for entry in chunk["lang"]:
if entry in counts_dict.keys():
counts_dict[entry] += 1
else:
counts_dict[entry] = 1
# Print the populated dictionary
print(counts_dict)
#输出结果
{'en': 97, 'et': 1, 'und': 2}