简体   繁体   中英

pandas dataframe creating columns with for loop

I'm trying to create new columns and add data from first dataframe every 1000 iterations through price. When i do head(), the program just ends with no error after 15 seconds

import pandas as pd
import matplotlib.pyplot as plt

data_frame = pd.read_csv('candle_data.csv', names=['Time', 'Symbol','Side', 'Size', 'Price','1','2','3','4','5'])
price_df = pd.DataFrame()

for price in data_frame['Price']:
    count_tick = 0
    count_candle = 0
    if count_tick < 1000:
        price_df[count_candle] = price
        count_tick +=1
    elif count_tick == 1000:
        count_tick = 0
        count_candle +=1

price_df.head()

Your count_tick and count_candle variables reset to 0 for each time you loop... You should pull them out of the for loop if you would like them to increment.

count_tick = 0
count_candle = 0
for price in data_frame['Price'].astype(int):
    if count_tick < 1000:
        price_df[count_candle] = price
        count_tick +=1
    elif count_tick == 1000:
        count_tick = 0
        count_candle +=1

price_df.head()

Not sure what you are looking to ultimately do...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM