简体   繁体   English

将包含长列表的Pandas df保存为csv文件

[英]Save Pandas df containing long list as csv file

I am trying to save a pandas dataframe as .csv file. 我正在尝试将pandas数据帧保存为.csv文件。 Currently my code looks like this: 目前我的代码如下所示:

with open('File.csv', 'a') as f:
        df.to_csv(f, header=False)

The saving works but the problem is that the lists in my dataframe are just compressed to [first,second,...,last] and all the entries in the middle are discarded. 保存有效,但问题是我的数据框中的列表只是压缩为[first,second,...,last],并且中间的所有条目都被丢弃。 If I just look at the original dataframe all entries are there. 如果我只看原始数据帧,那么所有条目都在那里。 Is there any way how I can convert the list to a string which contains all the elements (str(df) also discards the middle elements) or how I can save a full numpy array in a cell of a csv table? 有什么方法可以将列表转换为包含所有元素的字符串(str(df)也丢弃中间元素)或者如何在csv表的单元格中保存完整的numpy数组?

Thank you for your help, Viviane 谢谢你的帮助,Viviane

Your code should work properly. 您的代码应该正常工作。 I couldn't reproduce described behavior. 我无法重现描述的行为。

Here is a bit more "pandaic" version: 这里有一点“熊猫”版本:

df.to_csv('File.csv', header=False, mode='a')

PS pay attention at the mode='a' (append) parameter... PS注意mode='a' (追加)参数......

UPDATE: 更新:

How to get rid of ellipsis when displaying / printing a DF: 如何在显示/打印 DF时删除省略号:

with pd.option_context("display.max_columns", 0):
    print(df)

You can probably convert elements present in the list using join method. 您可以使用join方法转换列表中的元素。

example: 例:

lst =  ['Hello!','I','am', 'Pandas User','.']
strng = ' '.join(lst)
print (strng)

hope this helps to you. 希望这对你有所帮助。

I had issues while saving dataframes too. 我在保存数据帧时遇到了问题。 I had a dataframe in which some columns consisted of lists as its elements. 我有一个数据框,其中一些列包含列表作为其元素。 When I saved the datfarme using df.to_csv and then read it from disk using df.read_csv , the list and arrays were turned into a string of characters. 当我使用保存在datfarme df.to_csv ,然后使用从磁盘读取它df.read_csv ,列表和数组都变成了一串字符。 Hence [1,2,3] was transformed to '[1,2,3]' . 因此[1,2,3]被转化为'[1,2,3]' When I used HDF5 format the problem was solved. 当我使用HDF5格式时,问题就解决了。

If you dataframe is called df_temp , then you can use: 如果您将数据帧称为df_temp ,那么您可以使用:

store = pd.HDFStore('store.h5')
store['df'] = df_temp

to save the dataframe in HDF5 format and you can read it using the following command: 以HDF5格式保存数据帧,您可以使用以下命令读取它:

store = pd.HDFStore('store.h5')
df_temp_read = store['df']

You can look at this answer . 你可以看看这个答案 I should also mention that pickle did not work for me, since I lost the column names when reading from the file. 我还应该提一下,pickle对我不起作用,因为我从文件中读取时丢失了列名。 Maybe I did something wrong, but apart from that, pickle can cause compatibility issues if you plan to read the file in different python versions. 也许我做错了什么,但除此之外,如果你计划在不同的python版本中读取文件,pickle可能会导致兼容性问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM