我有一个DataFrame如下:
item_id | item_count | day | month
1 | 2 | 1 | 1
2 | 1 | 1 | 2
...
数据框说明item_id
是什么item_count
,day
中month
售出了多少item_count
。我想获取每个item_id
中每个month
中的item_id
个。我认为我必须对month
和const data = [
{
"style": "styleName_01",
"skus": [
{ "sku": "001", "color": "COLOR_01", "size": "SIZE_08" },
{ "sku": "002", "color": "COLOR_02", "size": "SIZE_07" },
{ "sku": "003", "color": "COLOR_02", "size": "SIZE_08" },
{ "sku": "004", "color": "COLOR_02", "size": "SIZE_12" }
]
},
{
"style": "styleName_02",
"skus": [
{ "sku": "005", "color": "COLOR_01", "size": "SIZE_05" },
{ "sku": "006", "color": "COLOR_03", "size": "SIZE_07" },
{ "sku": "007", "color": "COLOR_03", "size": "SIZE_08" },
{ "sku": "008", "color": "COLOR_02", "size": "SIZE_04" }
]
}
]
// flatten into an array of skus
const skus = data.flatMap(style => style.skus)
console.log('skus', skus)
// group by color
const colorsObj = skus.reduce((colors, sku) => {
if (!colors.hasOwnProperty(sku.color))
colors[sku.color] = [] // use Set if you want unique values
colors[sku.color].push(sku.size)
return colors
}, {})
console.log('colorsObj', colorsObj)
// create array from object entries
const colorsArr = Object.entries(colorsObj).map(([color, sizes], i) => {
return {
sku: i,
color,
sizes
}
})
console.log('colorsArr', colorsArr)
进行分组,但是第一个似乎给出了错误的结果。我该怎么办?
答案 0 :(得分:1)
我使用了groupby,但是我得到的结果似乎是您期望的-这就是我所做的。
df.groupby(['item_id ',' month']).sum()
这是您要找的吗?