将标题/列添加到我的 .csv 文件

骚扰

我目前的代码:

file='filelocation.sav'
finalfile = 'filelocation.csv'

s=spio.readsav(file, python_dict=True, verbose=True)
amf=np.asarray(s["amf"])

d=[amf]
df=pd.DataFrame(data=d)
df=df.T
df.to_csv(finalfile,sep= ' ', encoding = 'utf-u', header=True)

这是我目前的输出

我希望我的输出看起来像这样:

   amf
0  6.685
1  6.84
2  7.0
3  7.16
4  7.33
5  7.51
6  7.70

等。基本上我希望标题与它代表的数据对齐,以便我可以正确调用所述数据和绘图。

我注意到的另一个问题是,当我问 jupyter 时

file.columns

告诉我索引,我返回了这个输出:

Index([u' 0 1 2 3 4 5 6'], dtype='object)

当我想要 7 个特定索引(包括“0”)时,这让我相信只有一个索引被考虑在内。


在这里编辑过去:

所以:看起来我遇到的另一个问题是,虽然我打算拥有的行数长度约为 87000,但它显示为具有 87000 列。

看起来我需要对我的数组“amf”创建为行而不是列进行一些更改。

丹尼·梅塞霍

问题是您将 amf 包裹在括号之间,df=[amf]使其成为数组列表df=amf,例如将其更改为:

data = [np.array([6.685, 6.84, 7.0, 7.16, 7.33, 7.51, 7.70])]
result = pd.DataFrame(data=data)
print(result.shape)

返回:(1, 7)即 1 列 7 行,但是:

data = np.array([6.685, 6.84, 7.0, 7.16, 7.33, 7.51, 7.70])
result = pd.DataFrame(data=data)
print(result.shape)

返回(7, 1),您还需要设置以下列DataFrame

import numpy as np
import pandas as pd

data = np.array([6.685, 6.84, 7.0, 7.16, 7.33, 7.51, 7.70])
result = pd.DataFrame(data=data, columns=['amf'])
print(result)

输出:

   amf
0    1
1    2
2    3
3    4
4    5
5    6

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章