add a row index to pandas dataframe

682 views python

I have an option chain dataset that I'm trying to make sense of--an index would go a long way to being able to call value from it, sadly the data didn't come with an index. I cannot figure out how to add a new row to the top of this data frame, I tried groupby() and Multindex() but to no avail.

See the .head() value below, and the column values that I would like to use for the index in the notes.

In [3]:

import pandas as pd

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")

The data frame is too large to fit into this window, so I'm adding a screenshot.enter image description here

answered question

1 Answer


If I understand correctly, it's not an index you want to add, but column names. You can do this directly from 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)

posted this

Have an answer?


Please login first before posting an answer.