这是我之前提出的a question。你知道吗
如何转换以下行:
time1,stockA,bid,1
time2,stockA,ask,1.1
time3,stockB,ask,2.1
time4,stockB,bid,2.0
time5,stockA,bid,1.1
time6,stockA,ask,1.2
time7,stockA,high,1.5
time8,stockA,low,0.5
到以下panda dataframe
:
time stock bid ask high low
time1 stockA 1
time2 stockA 1.1
time3 stockB 2.1
time4 stockB 2.0
time5 stockA 1.1
time6 stockA 1.2
time7 stockA 1.5
time8 stockA 0.5
感谢您的帮助!你知道吗
我的方法是将csv读入2个df,一个有bid-ask列,一个没有bid-ask列:
对于第二个df,我们可以调用^{} 来旋转df以从'bid'值创建列,我们需要重置索引,然后我们可以将2个df合并在一起以获得您想要的结果,如果需要,您可以用空字符串替换NaN值:
你要做的是旋转桌子。 以下方法导致时间和库存形成一个多指标
我认为数据帧应该是这样的。 那就做吧
您可以使用
df.fillna
用您喜欢的任何东西填充nan。一般来说,将列值转换为列标题称为数据透视。.unstack
透视多重索引的一个级别。您也可以检查.pivot
。你能做到的去掉列中包含每列“prices”的外层
相关问题 更多 >
编程相关推荐