python-如何使用一列数组执行to_csv()



我的数据包含.csv中的一些大数组,如:

df = pd.DataFrame()
for x in range(1, 6):
data = {'a':x,
'b':np.array(range(1, 10000))}
df = df.append(data, ignore_index=True)
df.to_csv(f"./src/temp/test.csv", index=True)

数组被保存为:

0 1 [   1    2    3 ... 9997 9998 9999]
1 2 [   1    2    3 ... 9997 9998 9999]
2 3 [   1    2    3 ... 9997 9998 9999]
3 4 [   1    2    3 ... 9997 9998 9999]
4 5 [   1    2    3 ... 9997 9998 9999]

这使得以后阅读它们变得不可能。我该如何解决这个问题?

编辑:我本来打算稍后读,比如:

convert = lambda x: np.fromstring(x.strip('[]'), dtype=int, sep=' ')
df = pd.read_csv('./src/temp/test.csv',
converters={'b': convert},
index_col=0)

您可以使用set_printoptions():更改打印threshold的数字

threshold:触发摘要而非完整repr的数组元素总数(默认为1000(。要始终使用完整repr而不进行摘要,请通过sys.maxsize

np.set_printoptions(threshold=100000) # or threshold=sys.maxsize
df.to_csv('./src/temp/test.csv', index=True)

相关内容

最新更新