gpt4 book ai didi

python - 在 python 中取一个没有 numpy 或 pandas 的数组样本

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:06 24 4
gpt4 key购买 nike

我正在尝试创建一种“pythonic”方式,在 python 中获取一个非常大的数组的一小部分。

我目前正在接收一个包含 58 列和 4960 行的 csv,代码如下:

def import_normal_csv(file):
# Create blank array
results = []
# Open file
with open(file) as csvfile:
# read in file changing values to floats
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
results.append(row)
return results

def main():
print(" Working SPAM Dataset... ")
# Create a raw data array without numpy
spam_raw_data = import_normal_csv('spam.csv')

# CREATE SUBSET OF SPAM_RAW_DATA HERE

random.shuffle(spam_raw_data)

我已经看到使用 numpypandas 执行此操作的各种方法,但我想在没有这些库的情况下自然地执行此操作。而不是我庞大的数组,我怎么能只取...500 行(或某个明显少于近 5000 行的任意数字)?

最佳答案

你可以使用内置的random库,例如:

import random

random.sample(data, 500)

这将为您提供一个包含 500 个 listlist,每个代表一行。

关于python - 在 python 中取一个没有 numpy 或 pandas 的数组样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54878002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com