I've read here Creating an empty Pandas DataFrame, then filling it? that it is much better to append a list than appending a dataframe.
I have code that opens a csv as a dataframe, enters a loop of 10k+ contracts and wait for data from an API , append this data to a temporary dataframe, and save the end result to a csv. Below is my code. What would be the most efficient way to perform this in python?
Any other improvements not related to the use of dataframe vs list are also welcome.
df = pd.DataFrame()
try:
watchlistdf = pd.read_csv(
    mypath + "//Extracts//Options//Watchlist//volatility-watchlist-summary.csv"
)
except:
watchlistdf = pd.DataFrame()
for index, x in enumerate(tickers):
    print(
        "processing contract:",
        index,
        "of",
        len(tickers),
        "-",
        x.contract.localSymbol,
    )
    impliedVol = None
    delta = None
    undPrice = None
    while x.modelGreeks is None and z < 1000:
        z = z + 1
        ib.sleep()
    if x.modelGreeks is not None:
        impliedVol = x.modelGreeks.impliedVol
        delta = x.modelGreeks.delta
        undPrice = x.modelGreeks.undPrice
    symbol = x.contract.symbol
    localSymbol = x.contract.localSymbol
    strike = x.contract.strike
    right = x.contract.right
    expiration = (x.contract.lastTradeDateOrContractMonth,)
    last = float(x.last)
    close = float(x.close)
    bid = float(x.bid)
    ask = float(x.ask)
    if util.isNan(bid) or util.isNan(ask):
        mid = float("Nan")
    else:
        mid = (bid + ask) / 2
    bidaskspread = ask - bid
    if util.isNan(mid) or util.isNan(last):
        marketmakeredge = float("Nan")
    else:
        marketmakeredge = round(abs(mid - last))
    if util.isNan(last) or util.isNan(mid) or util.isNan(bidaskspread):
        side = ""
    else:
        side = np.where(
            last <= (mid - (0.1 * bidaskspread)),
            "Bid",
            np.where(last >= (mid + (0.1 * bidaskspread)), "Ask", "Mid"),
        )
    df = df.append(
        {
            "rundate": program_start,
            "symbol": symbol,
            "localSymbol": localSymbol,
            "strike": strike,
            "right": right,
            "expiration": expiration,
            "close": close,
            "bid": bid,
            "ask": ask,
            "mid": mid,
            "impliedVol": impliedVol,
            "delta": delta,
            "undPrice": undPrice,
            "bidaskspread": bidaskspread,
            "marketmakeredger": marketmakeredge,
            "side": side,
        },
        ignore_index=True,
    )
watchlistdf = pd.concat([watchlistdf, df])
watchlistdf.to_csv(
mypath + "//Extracts//Options//Watchlist//volatility-watchlist-options-details.csv")
