Convert time series format

大城市里の小女人 提交于 2019-12-13 08:03:54

问题


I have a dataset (df) of time series as follow:

date          symbol   close
09/01/2018    ACA      132.1
10/01/2018    ACA      134.4
...
28/04/2013    BA       22.12
...
21/01/2016    DIL      180.01
...

The idea was to convert it as this:

date          ACA      BA      DIL
28/04/2013    NaN      22.12   NaN
...
21/01/2016    NaN      23      180.01
...
...
...
10/01/2018    134.4    32.2    181.3

So taking the longest period of time on first column, and match the close price on the others. I guess it can be done with VLOOKUP() or MATCH() somehow.

Any idea?


回答1:


You can do this with Powerquery.

Step 1: Select a populated cell in your data range Goto PowerQuery tab if pre Excel 2016 (and have installed the free-add in) or Data tab in 2016 > Data > Get & Transform > from table

Step 2: Make sure date column is formatted as Date

Step 3: Order date column in ascending order

Step 4: Select symbol column > Transform tab > Pivot column

Ensure that values part is using Close

Step 5: Re-arrange columns as required

Step 6: Close and load to page

Note: You do not replace Null with NaN. When exported to sheet these cells will be blank.

M Code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"symbol", type text}, {"close", type number}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"date", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[symbol]), "symbol", "close", List.Sum),
    #"Reordered Columns" = Table.ReorderColumns(#"Pivoted Column",{"date", "ACA", "BA", "DIL"})
in
    #"Reordered Columns"


来源:https://stackoverflow.com/questions/50022087/convert-time-series-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!