我使用外汇数据集,试图用开放、高、低、收盘的每一个刻度来填充我的数据框。你知道吗
这是我的密码:
import pandas as pd
# pandas settings
pd.set_option('display.max_columns', 320)
pd.set_option('display.max_rows', 320)
pd.set_option('display.width', 320)
# creating dataframe
df = pd.read_csv('https://www.dropbox.com/s/tcek3kmleklgxm5/eur_usd_lastweek.csv?dl=1', names=['timestamp', 'ask', 'bid', 'avol', 'bvol'], parse_dates=[0], header=0)
df['spread'] = df.ask - df.bid
df['symbol'] = 'EURUSD'
times = pd.DatetimeIndex(df.timestamp)
# parameters for df.groupby()
df['date'] = times.date
df['hour'] = times.hour
# 1h candles updated every tick
df['candle_number'] = '...'
df['1h_open'] = '...'
df['1h_high'] = '...'
df['1h_low'] = '...'
df['1h_close'] = '...'
# print(df)
grouped = df.groupby(['date', 'hour'])
for idx, x in enumerate(grouped):
print(idx)
print(x)
如你所见,使用for循环,我得到了组。你知道吗
现在我想在我的数据帧中填充以下列:
我希望它不会太混乱() 干杯
重新编制日期和时间索引很方便:
df_new = df.set_index(['date', 'hour'])
然后应用groupby函数按索引聚合:
您可以
reset_index()
返回到平面数据帧。你知道吗相关问题 更多 >
编程相关推荐