问题
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