我正在使用一個結構如下的資料集。如您所見,指標列包含二進制分類資料。
country_code indicator cumulative_count
AFG cases 52909
AFG deaths 2230
... ... ...
我想將指標列變成兩個單獨的列(對應于指標的值:病例和死亡)。即我期望最終結果是這樣的:
country_code cases deaths
AFG 52909 2230
... ... ...
筆記:
- 原始資料集可從
uj5u.com熱心網友回復:
這也可以使用 Windows Excel 2010 和 Excel 365(Windows 或 Mac)中提供的 Power Query 來完成
使用 Power Query
- 將資料表加載到 Excel 中
- 在資料表中選擇一些單元格
Data => Get&Transform => from Table/Range
或者from within sheet
- 當 PQ 編輯器打開時:
Home => Advanced Editor
- 記下第 2 行中的表名
- 粘貼下面的 M 代碼代替您看到的內容
- 將第 2 行中的表名稱更改回最初生成的名稱。
- 閱讀評論并探索
Applied Steps
以了解演算法
let //Read in the table //Change table name in next line to your actual table name Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], //Remove the unneeded columns #"Removed Other Columns" = Table.SelectColumns(Source,{"country_code", "indicator", "year_week", "cumulative_count"}), //Set the data types for those columns #"Set Data Type" = Table.TransformColumnTypes(#"Removed Other Columns",{ {"country_code", type text}, {"indicator", type text},{"year_week", type text},{"cumulative_count", Int64.Type} }), //Pivot the Indicator column and aggregate by Sum #"Pivoted Column" = Table.Pivot(#"Set Data Type", List.Distinct(#"Removed Other Columns"[indicator]), "indicator", "cumulative_count", List.Sum), //Filter to show only the relevant year-week for rows where thiere is a country_code // (the others refer to continents) #"Filtered Rows" = Table.SelectRows(#"Pivoted Column", each ([country_code] <> null) and ([year_week] = "2020-53")) in #"Filtered Rows"
過濾后僅顯示 2020-53
uj5u.com熱心網友回復:
如果我正確理解你的問題。一種方法:在 $F$2 中添加新列 F 公式: sumifs($D2:$D$9999, $B2:$B$9999, $B2, $E2:$E$9999, "deaths")
通過“案例”的結束記錄過濾器列 E 向下復制公式
如果隨后在標題行上方插入行,則可以使用 Subtotal(109, ...) 查看特定年份的累積計數,或者使用 Sumif 添加另一列,如上所示
標籤:擅长