如何将分类值转换为 Excel 中的列?
How to convert categorical values into columns in Excel?
我正在使用结构如下所示的数据集。如您所见,指标 列包含二进制分类数据。
country_code indicator cumulative_count
AFG cases 52909
AFG deaths 2230
... ... ...
我想将 indicator 列变成两个单独的列(对应于 indicator 的值:病例 和 死亡 )。 IE。我希望最终结果是这样的:
country_code cases deaths
AFG 52909 2230
... ... ...
备注:
- 原始数据集可从 ECDC website.
公开访问
- 我只对一个特定的 year_week (2020-53) 的 cumulative_count 感兴趣。
- 这是数据集的屏幕截图:
如果我没有正确理解你的问题。单程:
添加新列 F
$F$2 中的公式:sumifs($D2:$D$9999, $B2:$B$9999, $B2, $E2:$E$9999, "deaths")
将公式向下复制到结束记录
为“案例”筛选 E 列
如果随后在 header 行上方插入行,则可以使用 Subtotal(109, ...) 查看特定年份的累计计数,或者使用 Sumif 添加另一列,如上所示
这也可以使用 Power Query 完成,在 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)
使用 Power Query
- 将您的数据 table 加载到 Excel
- Select 数据中的某个单元格 Table
Data => Get&Transform => from Table/Range
或 from within sheet
- 当 PQ 编辑器打开时:
Home => Advanced Editor
- 记下第 2 行中的 Table 名称
- 粘贴下面的 M 代码代替您看到的内容
- 将第 2 行中的 Table 名称更改回最初生成的名称。
- 阅读评论并探索
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
我正在使用结构如下所示的数据集。如您所见,指标 列包含二进制分类数据。
country_code indicator cumulative_count
AFG cases 52909
AFG deaths 2230
... ... ...
我想将 indicator 列变成两个单独的列(对应于 indicator 的值:病例 和 死亡 )。 IE。我希望最终结果是这样的:
country_code cases deaths
AFG 52909 2230
... ... ...
备注:
- 原始数据集可从 ECDC website. 公开访问
- 我只对一个特定的 year_week (2020-53) 的 cumulative_count 感兴趣。
- 这是数据集的屏幕截图:
如果我没有正确理解你的问题。单程: 添加新列 F $F$2 中的公式:sumifs($D2:$D$9999, $B2:$B$9999, $B2, $E2:$E$9999, "deaths")
将公式向下复制到结束记录 为“案例”筛选 E 列
如果随后在 header 行上方插入行,则可以使用 Subtotal(109, ...) 查看特定年份的累计计数,或者使用 Sumif 添加另一列,如上所示
这也可以使用 Power Query 完成,在 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)
使用 Power Query
- 将您的数据 table 加载到 Excel
- Select 数据中的某个单元格 Table
Data => Get&Transform => from Table/Range
或from within sheet
- 当 PQ 编辑器打开时:
Home => Advanced Editor
- 记下第 2 行中的 Table 名称
- 粘贴下面的 M 代码代替您看到的内容
- 将第 2 行中的 Table 名称更改回最初生成的名称。
- 阅读评论并探索
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