向pandas数据框添加行索引

时间:2018-09-16 02:11:43

标签: python pandas

我有一个要理解的期权链数据集-索引要能够从中调用值很长的路要走,可悲的是数据没有索引。我无法弄清楚如何在此数据框的顶部添加新行,但尝试了groupby()和Multindex()却无济于事。

请参阅下面的.head()值,以及我想在注释中用于索引的列值。

In [3]:

import pandas as pd

"""
Columns:
ULSymbol, ULPrice, Flag, OptionSymbol, Ext, Type, Expiry, Date, Strike, Last, Bid, Ask, Volume, OI, IV, Delta, Gamma, Theta, Vega, AKA
"""
​
opt_chain = pd.read_csv("market_prices/2018-08-21.csv")
opt_chain.head()

数据框太大,无法容纳在此窗口中,因此我要添加屏幕截图。enter image description here

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那不是您要添加的索引,而是列名。您可以直接从pd.read_csv执行此操作:

cols = ["ULSymbol", "ULPrice", "Flag", "OptionSymbol", "Ext", "Type", "Expiry", "Date", "Strike", "Last", "Bid", "Ask", "Volume", "OI", "IV", "Delta", "Gamma", "Theta", "Vega", "AKA"]

opt_chain = pd.read_csv("market_prices/2018-08-21.csv", header=None, names = cols)