#foo.csv
No, Fruit, Quality, Units, Location
1, Orange, New, 10, Village
2, Orange, Fresh, 20, City
3, Apple, New, 15, Village
4, Grapes, Fresh, 25, City
5, Apple, Fresh, 30, City
6, Apple, Fresh, 35, City
Run Code Online (Sandbox Code Playgroud)
有了这些数据,我想以下列方式输出:
水果 | 位置 | 质量 | 单位
我无法进一步分组(子组?)数据,我带来的内容如下,但这不是我需要的:
Import-Csv "E:\foo.csv" |
Group-Object { $_.Fruit } |
Select-Object -Property Count,
@{ Name = 'Fruits'; Expression = { $_.Name } },
@{ Name = 'Location'; Expression = { ($_.Group | Measure-Object -Property Units -Sum).Sum } } |
Export-Csv new.csv -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)
我进一步尝试:
Import-Csv "E:\foo.csv" |
Group-Object { $_.Fruit } |
Select-Object -Property Count,
@{ Name = 'Fruits'; Expression = { $_.Name } },
@{ Name = 'Location'; Expression = { $_.Location } },
@{ Name = 'Quality'; Expression = { $_.Quality } },
@{ Name = 'Units'; Expression = { ($_.Group | Measure-Object -Property Units -Sum).Sum } } | Sort-Object -Property Count | Format-Table -Auto
Run Code Online (Sandbox Code Playgroud)
但是数据是空白的。
您可以同时按多个属性分组:
Import-Csv "E:\foo.csv"|
Group-Object Fruit,Location,Quality|
Select-Object @{Name='Fruit' ;Expression={$_.Values[0]}},
@{Name='Location';Expression={$_.Values[1]}},
@{Name='Quality' ;Expression={$_.Values[2]}},
@{Name='Units' ;Expression={
($_.Group|Measure-Object Units -Sum).Sum
}}
Run Code Online (Sandbox Code Playgroud)