import polars as pl
df = pl.DataFrame({
'A': range(1,4),
'B': range(1,4),
'C': range(1,4),
'D': range(1,4)
})
print(df)
shape: (3, 4)
┌─────┬─────┬─────┬─────┐
│ A ┆ B ┆ C ┆ D │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╡
│ 1 ┆ 1 ┆ 1 ┆ 1 │
│ 2 ┆ 2 ┆ 2 ┆ 2 │
│ 3 ┆ 3 ┆ 3 ┆ 3 │
└─────┴─────┴─────┴─────┘
df_melt = df.melt(
variable_name="recipe",
value_name="revenue")
print(df_melt)
shape: (12, 2)
┌────────┬─────────┐
│ recipe ┆ revenue │
│ --- ┆ --- │
│ str ┆ i64 │
╞════════╪═════════╡
│ A ┆ 1 │
│ A ┆ 2 │
│ A ┆ 3 │
│ B ┆ 1 │
│ B ┆ 2 │
│ … ┆ … │
│ C ┆ 2 │
│ C ┆ 3 │
│ D ┆ 1 │
│ D ┆ 2 │
│ D ┆ 3 │
└────────┴─────────┘
似乎我需要添加一个索引,以便将df_melt
旋转回原始形式df
?有没有办法在不添加索引的情况下旋转一个极地框架?
df_melt = df_melt.with_columns(index=pl.col("revenue").cum_count().over("recipe"))
df_melt.pivot(
index='index',
columns='recipe',
values='revenue',
aggregate_function='first'
)
shape: (3, 5)
┌───────┬─────┬─────┬─────┬─────┐
│ index ┆ A ┆ B ┆ C ┆ D │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═══════╪═════╪═════╪═════╪═════╡
│ 1 ┆ 1 ┆ 1 ┆ 1 ┆ 1 │
│ 2 ┆ 2 ┆ 2 ┆ 2 ┆ 2 │
│ 3 ┆ 3 ┆ 3 ┆ 3 ┆ 3 │
└───────┴─────┴─────┴─────┴─────┘
在R中,我可以执行等同于melt和pivot而不带索引,并且在Python中寻求相同的功能.
df_pandas = df.to_pandas()
library(tidyverse)
library(reticulate)
df_long <-
py$df_pandas |>
pivot_longer(
everything(),
names_to = 'recipe',
values_to = 'value'
)
df_long |>
pivot_wider(
names_from='recipe',
values_from='value'
) |>
unnest(cols = c(A,B,C,D))