python - 有相当于潮汐巢功能的 Pandas 吗?

标签 python r pandas tidyverse

R语言中的tidyr::unnest方法在 Pandas 中等效,它称为explode,如this very detailed answer中所述。
我想知道是否有与̀tidyr::nest`方法等效的方法。

示例R代码:

library(tidyr)
iris_nested <- as_tibble(iris) %>% nest(data=-Species)

数据列是一个列表列,其中包含数据框(例如,在运行多个模型时,这对于建模很有用)。
iris_nested
# A tibble: 3 x 2
  Species              data
  <fct>      <list<df[,4]>>
1 setosa           [50 × 4]
2 versicolor       [50 × 4]
3 virginica        [50 × 4]

要访问数据列中的一个元素:
iris_nested[1,'data'][[1]]
[...]
# A tibble: 50 x 4
   Sepal.Length Sepal.Width Petal.Length Petal.Width
          <dbl>       <dbl>        <dbl>       <dbl>
 1          5.1         3.5          1.4         0.2
 2          4.9         3            1.4         0.2
 3          4.7         3.2          1.3         0.2
 4          4.6         3.1          1.5         0.2
 5          5           3.6          1.4         0.2
 6          5.4         3.9          1.7         0.4
 7          4.6         3.4          1.4         0.3
 8          5           3.4          1.5         0.2
 9          4.4         2.9          1.4         0.2
10          4.9         3.1          1.5         0.1
# … with 40 more rows
library(tidyr)
iris_nested <- as_tibble(iris) %>% nest(data=-Species)
iris_nested
iris_nested[1,'data'][[1]]

示例python代码:
import seaborn
iris = seaborn.load_dataset("iris")

如何将这个数据框嵌套在pandas中:
  • 首先以一种不太复杂的方式(与pandas explode功能保持一致),数据列包含一个简单的列表
  • 其次,数据列包含数据帧,如上面的示例
  • 所示

    最佳答案

    我认为这是最接近的:

    df=iris.groupby("Species").apply(lambda x:dict(x))
    

    输出:
    Species
    setosa        {'Sepal.Length': [5.1, 4.9, 4.7, 4.6, 5.0, 5.4...
    versicolor    {'Sepal.Length': [7.0, 6.4, 6.9, 5.5, 6.5, 5.7...
    virginica     {'Sepal.Length': [6.3, 5.8, 7.1, 6.3, 6.5, 7.6...
    

    要访问一种物种:
    pd.DataFrame(df['setosa'])
    
    
         Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
    100           5.1          3.5           1.4          0.2  setosa
    101           4.9          3.0           1.4          0.2  setosa
    102           4.7          3.2           1.3          0.2  setosa
    103           4.6          3.1           1.5          0.2  setosa
    104           5.0          3.6           1.4          0.2  setosa
    105           5.4          3.9           1.7          0.4  setosa
    106           4.6          3.4           1.4          0.3  setosa
    107           5.0          3.4           1.5          0.2  setosa
    108           4.4          2.9           1.4          0.2  setosa
    109           4.9          3.1           1.5          0.1  setosa
    110           5.4          3.7           1.5          0.2  setosa
    111           4.8          3.4           1.6          0.2  setosa
    112           4.8          3.0           1.4          0.1  setosa
    113           4.3          3.0           1.1          0.1  setosa
    114           5.8          4.0           1.2          0.2  setosa
    115           5.7          4.4           1.5          0.4  setosa
    116           5.4          3.9           1.3          0.4  setosa
    117           5.1          3.5           1.4          0.3  setosa
    118           5.7          3.8           1.7          0.3  setosa
    119           5.1          3.8           1.5          0.3  setosa
    120           5.4          3.4           1.7          0.2  setosa
    121           5.1          3.7           1.5          0.4  setosa
    122           4.6          3.6           1.0          0.2  setosa
    123           5.1          3.3           1.7          0.5  setosa
    124           4.8          3.4           1.9          0.2  setosa
    

    关于python - 有相当于潮汐巢功能的 Pandas 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59068394/

    相关文章:

    python - 如何使用 python twisted 从服务器向客户端发送数据?

    python - 为什么 Z3 对于微小的搜索空间很慢?

    Python 忽略标点符号和空格

    python - 单独解决,稍后再安装Conda环境

    r - 如何绘制具有置信区间的数据?

    r - 田口交叉阵列设计创建和分析

    r - summary.manova 中的错误 - 残差有等级顺序缺陷

    python - 如何将选定列的值存储在单独的行中?

    json - 如何将 Pandas 系列转换为所需的 JSON 格式?

    Python:如何缩短 pandas 列中的整数?