在熊猫中将长列表保存到csv中

时间:2020-12-25 05:36:45

标签: python pandas csv

我正在尝试将一个列表(有 7000 个值)保存到一个 csv 文件中。但是当我打开 csv 时,列表会像这样被截断 '[1 2 3 ... 6999 7000]' 并且也存储为字符串。有没有办法在 csv 中存储一长串列表而不会截断值。

x = []
a = np.arange(0,7000,1)
x.append(a)
b  = np.arange(7001,14000,1)
x.append(b)
x

Out: [array([   0,    1,    2, ..., 6997, 6998, 6999]),
 array([ 7001,  7002,  7003, ..., 13997, 13998, 13999])]


df = pd.DataFrame({"x":x})
df.to_csv("x.csv")
df = pd.read_csv("x.csv")
df["x"][0]

Out: '[   0    1    2 ... 6997 6998 6999]'

type(df["x"][0])
Out: str

2 个答案:

答案 0 :(得分:0)

如果要将数据保存到csv,只需将数据类型转换为字符串str即可。

import pandas as pd
import numpy as np
alist = []
a = np.arange(0,7000,1)
alist.append(a)
b  = np.arange(7001,14000,1)
alist.append(b)
df = pd.DataFrame({"alist":alist})

# merge data as string
df['alist'] = df['alist'].map(lambda x: ','.join(map(str, x)))
df.to_csv("list.csv", index=False)

读取 csv 文件:

dfn = pd.read_csv("list.csv")
dfn['alist'] = dfn['alist'].str.split(',')
dfn['alist'] = dfn['alist'].map(lambda x: list(map(int, x)))
dfn['alist'][0]

或者只是考虑另一种方式:

# Examples
# For the simplest code, use the dump() and load() functions.

import pickle

# An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3, 4+6j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}

with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
# The following example reads the resulting pickled data.

with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)

答案 1 :(得分:0)

因为numpy数组的字符串表示被截断了。另一种方法是在将 numpy 数组保存到 csv 文件之前将其转换为 python 列表。

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'long_list': [np.arange(0, 7000).tolist()]
})

df.to_csv('temp.csv')