我一直试图将两个数据文件中的信息合并到一个新的数据文件中,而没有运气。我搜索了很多次,但仍然找不到相关的答案,所以如果我在搜索中漏掉了,很抱歉。
在创建投资策略时,在大量货币(超过50种)中,我选择了每个日期(以top_n.csv)投资的前5种货币,以及它们在每个日期(以weights.csv)投资每种货币的百分比权重。
top_n.csv的厕所,如:
Date 0 1 2 3 4
Aug 12, 2016 bitcoin ethereum 0 0 0
Aug 11, 2016 bitcoin ethereum ripple steem litecoin
Aug 10, 2016 bitcoin ethereum ripple 0 0
Aug 09, 2016 bitcoin ethereum steem ripple ethereum-classicweights.csv的厕所,如:
Date 0 1 2 3 4
Aug 12, 2016 0.859 0.089 nan nan nan
Aug 11, 2016 0.856 0.092 0.020 0.016 0.016
Aug 10, 2016 0.853 0.093 0.020 nan nan
Aug 09, 2016 0.858 0.086 0.020 0.020 0.017我试图填充的DataFrame包含相同的日期(在索引中),但是有许多列对应于一组更大的硬币(超过50枚),就像在W.csv中一样。
是否有一种有效的方法(对于每个日期)对任何有任何货币的货币填充正确的权重,而将其他货币的权重设为0?棘手的部分是在没有足够的货币时处理日期(因此top_n.csv的货币少于n种货币,而weights.csv在各自的位置上有nans )。
W.csv的厕所,如:
Date bitcoin ethereum bitcoin-cash ripple litecoin dash neo nem monero ethereum-classic iota qtum omisego lisk cardano zcash bitconnect tether stellar ....
Aug 12, 2016 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ....
Aug 11, 2016 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ....
Aug 10, 2016 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ....
Aug 09, 2016 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ....我的目标是生成一个看起来类似于DataFrame的W_all_target,我附加了一个看起来不正确的W_all_target(我已经为这个问题手工编辑了它)。
我已经保存了三个指示性的CSV,因为它将有助于检查它们。https://drive.google.com/open?id=1olx9ARI0XP5mqbqF1pfRfJyl9wIEWyZj
我还在学习,所以我知道这可能是一个简单的问题。真诚的感谢!!
发布于 2017-11-12 03:26:28
选项0
这是为了容纳零和nans。
dates = top_n.index.repeat(top_n.shape[1])
currs = top_n.values.ravel()
wghts = weights.values.ravel()
mask = currs != '0'
reshaped = pd.Series(wghts[mask], [dates[mask], currs[mask]]).unstack(fill_value=0)
W.update(reshaped)选项1
reshaped = pd.concat([d.stack() for d in [top_n, weights]], axis=1) \
.reset_index(1, drop=True).set_index(0, append=True)[1].unstack(fill_value=0)
reshaped
0 bitcoin ethereum ethereum-classic litecoin ripple steem
Date
2016-08-09 0.858 0.086 0.017 0.000 0.02 0.020
2016-08-10 0.853 0.093 0.000 0.016 0.02 0.018
2016-08-11 0.856 0.092 0.000 0.016 0.02 0.016
2016-08-12 0.859 0.089 0.000 0.016 0.02 0.015选项2
reshaped = pd.Series(
weights.values.ravel(),
[top_n.index.repeat(top_n.shape[1]), top_n.values.ravel()]
).unstack(fill_value=0)
reshaped
bitcoin ethereum ethereum-classic litecoin ripple steem
Date
2016-08-09 0.858 0.086 0.017 0.000 0.02 0.020
2016-08-10 0.853 0.093 0.000 0.016 0.02 0.018
2016-08-11 0.856 0.092 0.000 0.016 0.02 0.016
2016-08-12 0.859 0.089 0.000 0.016 0.02 0.015然后,您应该能够用
W.update(reshaped)
W
bitcoin ethereum bitcoin-cash ripple litecoin dash neo nem monero ethereum-classic iota qtum omisego lisk cardano zcash bitconnect tether stellar
Date
2016-08-12 0.859 0.089 0 0.02 0.016 0 0 0 0 0.000 0 0 0 0 0 0 0 0 0
2016-08-11 0.856 0.092 0 0.02 0.016 0 0 0 0 0.000 0 0 0 0 0 0 0 0 0
2016-08-10 0.853 0.093 0 0.02 0.016 0 0 0 0 0.000 0 0 0 0 0 0 0 0 0
2016-08-09 0.858 0.086 0 0.02 0.000 0 0 0 0 0.017 0 0 0 0 0 0 0 0 0https://stackoverflow.com/questions/47245071
复制相似问题