热线电话:13121318867

登录
2019-03-11 阅读量: 747
将数据帧的第0行设置为标题

我有一个dataframe(df),如下所示:

0 1 2 3 \

0 date BBG.XASX.ABP.S_price BBG.XASX.ABP.S_pos BBG.XASX.ABP.S_trade

1 2017-09-11 2.8303586 0.0 0.0

2 2017-09-12 2.8135189 98570.0 98570.0

3 2017-09-13 2.7829274 98570.0 0.0

4 2017-09-14 2.7928042 98570.0 0.0

4 5

0 BBG.XASX.ABP.S_cost BBG.XASX.ABP.S_pnl_pre_cost

1 -0.0 0.0

2 -37.439355326355 0.0

3 -0.0 -3015.4041549999965

4 -0.0 973.5561759999837

并df.column有一套:

Int64Index([ 0, 1, 2, 3, 4, 5], dtype='int64')

有人能告诉我如何修改数据帧,以便第0列是标题行吗?所以数据框看起来像:

date BBG.XASX.ABP.S_price BBG.XASX.ABP.S_pos BBG.XASX.ABP.S_trade

0 2017-09-11 2.8303586 0.0 0.0

1 2017-09-12 2.8135189 98570.0 98570.0

2 2017-09-13 2.7829274 98570.0 0.0

3 2017-09-14 2.7928042 98570.0 0.0

BBG.XASX.ABP.S_cost BBG.XASX.ABP.S_pnl_pre_cost

0 -0.0 0.0

1 -37.439355326355 0.0

2 -0.0 -3015.4041549999965

3 -0.0 973.5561759999837

解决办法:创建列表列表并传递给DataFrame构造函数所有列表,而不是先out[1:]使用列名称out[0]:

out = []

with lzma.open(subdirname, mode='rt') as file:

print(subdirname)

for line in file:

items = line.split(",")

out.append(items)

a = pd.DataFrame(out[1:], columns=out[0])

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子