【发布时间】:2021-03-05 04:58:52
【问题描述】:
我正在使用 pandas 和 numpy 进行特征提取。完成这个任务需要很长时间,所以我想保存 DataFrame 以备后用。
我将一个包含多个二维 numpy 数组的大型 pandas.Dataframe 写入 csv 文件。这些单元格值是这样的:
color contrast dissimilarity \
0 134.000000 [[0.0]] [[0.0]]
1 135.133333 [[0.16000000000000003]] [[0.16000000000000003]]
然后我从csv文件中读取数据,浮点数的格式变成了这样:
color contrast dissimilarity
0 134.00 [[0.]] [[0.]]
1 135.13 [[0.16]] [[0.16]]
浮点值“0.0”变为“0”。 .因此,当我使用从 csv 文件中读取的数据框作为模型的参数时,它会引发错误:
ValueError: could not convert string to float: '[[0.]]'
这就是我将 df 写入 csv 文件的方式: 从日期时间导入日期时间
now = datetime.now()
current_time = now.strftime("%x%H:%M:%S")
print("Current Time =", current_time)
current_time = current_time.replace(':', '')
current_time = current_time.replace('/', '')
compression_opts = dict(method='zip', archive_name= current_time + '.csv')
df.to_csv(current_time + 'test.zip', index=False, compression=compression_opts)
这就是我读取文件的方式
df2 = pd.read_csv('112220153048.csv', sep=',')
有没有办法在将数据写入cvs文件时不改变数字格式?
【问题讨论】:
-
这与
0.0与0.无关。该错误是因为read_csv根本不理解数组。 -
如果您想在 DataFrame 单元格中存储 NumPy 数组等复杂对象,CSV 不是您应该用来序列化 DataFrame 的格式。
-
@user2357112supportsMonica 那么我将使用什么格式??