[英]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.我正在尝试通过价格每 1000 次迭代创建新列并从第一个 dataframe 添加数据。 When i do head(), the program just ends with no error after 15 seconds
当我执行 head() 时,程序在 15 秒后结束,没有错误
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 和 count_candle 变量都会重置为 0...如果您希望它们增加,您应该将它们拉出 for 循环。
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() price_df.head()
Not sure what you are looking to ultimately do...不确定您最终要做什么...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.