dataframe - 使用 Julia 组合和透视 DataFrame

标签 dataframe pivot-table julia

我正在尝试读取两个 csv 文件(客户购买数据、产品数据)作为数据框,然后合并和旋转。

例子:

Customer Purchase Data:
CustomerID ProductId
1          39
1          6
2          8
3          39
3          40

Product Data:
ProductId Name
6         Car
8         House
39        Plane
40        Boat

Desired Pivot Table
ProductId Name  Cust_1 Cust_2 Cust_3
6         Car   1      0      0
8         House 0      1      0
39        Plane 1      0      1
40        Boat  0      0      1

我的问题是: 这个可以吗?
应该做吗?我可以在 Excel 中旋转它并将其保存为 csv。

最佳答案

这是另一种分两步的方法。

第 1 步:连接两个表

using DataFrames

### Create the DataFrame
customer = DataFrame(customerid = [1, 1, 2, 3, 3],
                     productid = [39, 6, 8, 39, 40])

product = DataFrame(productid = [6, 8, 39, 40],
                    name = ["Car", "House", "Plane", "Boat"])


res = join(customer, product, on = :productid)
# 5x3 DataFrames.DataFrame
# | Row | customerid | productid | name    |
# |-----|------------|-----------|---------|
# | 1   | 1          | 6         | "Car"   |
# | 2   | 2          | 8         | "House" |
# | 3   | 1          | 39        | "Plane" |
# | 4   | 3          | 39        | "Plane" |
# | 5   | 3          | 40        | "Boat"  |

第2步::添加一个带有“1”的虚拟列并取消堆叠DataFrame(从长格式移动到宽格式)

### Add dummy column
res[:tmp] = 1
res
# 5x4 DataFrames.DataFrame
# | Row | customerid | productid | name    | tmp |
# |-----|------------|-----------|---------|-----|
# | 1   | 1          | 6         | "Car"   | 1   |
# | 2   | 2          | 8         | "House" | 1   |
# | 3   | 1          | 39        | "Plane" | 1   |
# | 4   | 3          | 39        | "Plane" | 1   |
# | 5   | 3          | 40        | "Boat"  | 1   |


### Pivot from long to Wide
res = unstack(res, :customerid, :tmp)
# 4x5 DataFrames.DataFrame
# | Row | productid | name    | 1  | 2  | 3  |
# |-----|-----------|---------|----|----|----|
# | 1   | 6         | "Car"   | 1  | NA | NA |
# | 2   | 8         | "House" | NA | 1  | NA |
# | 3   | 39        | "Plane" | 1  | NA | 1  |
# | 4   | 40        | "Boat"  | NA | NA | 1  |


### Finally we can replace NA by 0
[res[isna(res[col]), col] = 0 for col in [symbol("1"), 
                                          symbol("2"), 
                                          symbol("3")]]
res
# 4x5 DataFrames.DataFrame
# | Row | productid | name    | 1 | 2 | 3 |
# |-----|-----------|---------|---|---|---|
# | 1   | 6         | "Car"   | 1 | 0 | 0 |
# | 2   | 8         | "House" | 0 | 1 | 0 |
# | 3   | 39        | "Plane" | 1 | 0 | 1 |
# | 4   | 40        | "Boat"  | 0 | 0 | 1 |

如果要更改列名,可以手动进行

names!(res, [:productid, :name, :cust_1, :cust_2, :cust_3])

关于dataframe - 使用 Julia 组合和透视 DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650883/

相关文章:

python - Pandas 数据帧 : How to groupby on a groupby?

python - Pandas DataFrame 乘以数组

excel - 应用了数据透视表标签过滤器,但实际上并未过滤数据

julia - 在 Julia 中销毁类型

http - 将 Julia `download` 的结果传递给内存而不是文件?

r - 对于大量列,避免将标签一一分配给数据框列

python - 将 Python 字典打印为并排多列的 Pandas value_counts

excel - 更新 Excel 中多个数据透视表上的数据源

excel - 如何更改 EPPlus 中 ColumnField 列标题的格式?

julia - Julia 中的二阶 ODE 给出了错误的结果