pandas 使用字符串标题将 numpy 数组保存到 csv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20306608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save a numpy array to csv with a string header
提问by Rodolphe
I have a large-ish numpy array containing floats, which I save as a csv file with np.savetxt("myFile.csv", myArray, delimiter=",")
我有一个包含浮点数的大型 numpy 数组,我使用 np.savetxt("myFile.csv", myArray, delimiter=",") 将其另存为 csv 文件
Now, as the array has many columns and it can become hard to remember what is what, I want to add a string header to the array before exporting it. Since numpy doesn't accept strings in float arrays, is there a trick to accomplish this?
现在,由于数组有很多列并且很难记住什么是什么,我想在导出数组之前向数组添加一个字符串标题。由于 numpy 不接受浮点数组中的字符串,是否有实现此目的的技巧?
[Solution] Thanks to Cyborg's advice, I managed to make this work installing Pandas.
[解决方案] 感谢 Cyborg 的建议,我设法通过安装 Pandas 完成了这项工作。
import pandas as pd
df = pd.DataFrame(A) # A is a numpy 2d array
df.to_excel("A.xls", header=C,index=False) # C is a list of string corresponding to the title of each column of A

