使用问题中描述的约束,这就是我的处理方式:
import numpy as np
import pandas as pd
data1 = np.array([[1,2,3],[4,5,6],[7,8,9]]) # list of lists in declaration
data2 = np.array([10,11,12])
data3 = np.array([13,14,15])
df = pd.DataFrame(zip(data1, data2, data3), columns=['data1', 'data2', 'data3'], dtype='str')
df['data1'] = df['data1'].str.replace('[', '').str.replace(']', '')
df.to_csv('./out.csv', index=False) # saving to file in cwd with name 'out.csv'
导致out.csv 包含:
data1,data2,data3
1 2 3,10,13
4 5 6,11,14
7 8 9,12,15
编辑:
回答以下关于从上述格式的 csv 转换回原始数组的评论:
# let pandas infer the data types (data1: str, data2: int, data3: int. By default)
df = pd.read_csv('./out.csv')
# convert each entry in data1 to a numpy array using fromstring method
df['data1'] = df['data1'].apply(lambda x : np.fromstring(x, sep=' '))
# nuance to get series of arrays back to numpy.ndarray
data1 = np.array(df['data1'].to_list())
# simply use to_numpy method for integer columns
data2 = df['data2'].to_numpy()
data3 = df['data3'].to_numpy()