Skip to content
Advertisement

Pandas DataFrame adding two zeros

Hi can some one explain why it adds two 0 0 to my data frame in this function

def ToDF(ticker):
    marketPriceP = []
    marketPriceT = []    
    marketPriceT.append(t())
    marketPriceP.append(currentPrice(ticker))
    while True:
        marketPriceT.append(t())
        marketPriceP.append(currentPrice(ticker))
        if len(marketPriceP) > 5:
            MKpriceDF = pd.DataFrame(['Price'])
            MKpriceDF1 = pd.DataFrame(['Time'])
            MKpriceDF = MKpriceDF.append(marketPriceP, ignore_index= True, verify_integrity= False, sort= None)
            MKpriceDF1 = MKpriceDF1.append(marketPriceT, ignore_index= True, verify_integrity= False, sort= None)
            MKpriceDF = pd.concat([MKpriceDF1, MKpriceDF], axis= 1)
            return MKpriceDF
            break

the output looks like

          0      0
0      Time  Price
1  22:24:52  41.04
2  22:24:52  41.04
3  22:24:52  41.04
4  22:24:52  41.04
5  22:24:52  41.04
6  22:24:52  41.04

Advertisement

Answer

You may want to revisit how you are creating the dataframe. Here are some changes for you to consider. I have limited information about what you are doing so my answer is catering to just the code I have seen.

def ToDF(ticker):
    marketPriceP = []
    marketPriceT = []    
    marketPriceT.append(t())
    marketPriceP.append(currentPrice(ticker))
    while True:
        marketPriceT.append(t())
        marketPriceP.append(currentPrice(ticker))
        if len(marketPriceP) > 5:
            MKpriceDF = pd.DataFrame({'Price':marketPriceP, 'Time':marketPriceT})
            return MKpriceDF

        #    MKpriceDF = pd.DataFrame(['Price'])
        #    MKpriceDF1 = pd.DataFrame(['Time'])
        #    MKpriceDF = MKpriceDF.append(marketPriceP, ignore_index= True, verify_integrity= False, sort= None)
        #    MKpriceDF1 = MKpriceDF1.append(marketPriceT, ignore_index= True, verify_integrity= False, sort= None)
        #    MKpriceDF = pd.concat([MKpriceDF1, MKpriceDF], axis= 1)
        #    return MKpriceDF
        #    break
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement