pandas to_csv通过在数据帧单元格中添加\ n换行符或存储numpy数组来改变数据

时间:2018-03-19 23:33:51

标签: python pandas csv dataframe

我正在生成一个带有一些数据的pandas数据帧(一些是numpy数组)并使用pandas.to_csv函数保存数据。

但是,当使用pandas.read_csv再次将csv文件读取到数据帧时,我注意到pandas在numpy数组中添加了换行符(参见上一个输出)

import pandas as pd
import numpy as np


# In[34]:
# create the dataframe
d = {'col1': [1, 2], 'col2': [3, 4]}
df=pd.DataFrame(data=d)
df.head()

输出:

   col1  col2
0   1   3
1   2   4
# In[35]:
# append array data to dataframe
data = np.array([])
data = np.zeros(512)
df = df.append({'col1' : data },  ignore_index=True)
df.head()


# In[37]:
# write to csv
df.to_csv('records.csv')

#read csv
df= pd.read_csv('records.csv')
df.head()


# In[40]:
array = df['col1'].values
print(array)

缺货[]:  ['1''2'  “[0。 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.80 0. 0-0.0.0.0.0.0.0.0.0.0。 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.00,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0 0. 0-0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0。]

如何解决这个或为什么会发生这种情况?

PS。感谢您的评论,我提供了一个复制问题并重新表达问题的示例,因为它似乎评论我要做的是将numpy数组存储在数据框单元格中。

1 个答案:

答案 0 :(得分:1)

这就是我们解决问题的方法。

array_list = np.array([])
for i in array:
    data_tmp = np.fromstring(i[1:-1],dtype=np.float,sep=' ')
    array_list = np.concatenate([array_list, data_tmp])
array_list = array_list.reshape((1,-1))

print(array_list)

[OUT]

[[0。 0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0   0. 0-0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0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