【问题标题】:Is there a pandas equivalent to the tidyr nest function?是否有相当于 tidyr 巢功能的熊猫?
【发布时间】:2020-03-22 21:40:17
【问题描述】:

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 中嵌套这个数据框:

  1. 首先以一种不太复杂的方式(与pandas explode 功能相比)数据列包含一个简单列表
  2. 其次,数据列包含如上例所示的数据帧

【问题讨论】:

    标签: python r pandas tidyverse


    【解决方案1】:

    我认为这是最接近的:

    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
    

    【讨论】:

      【解决方案2】:

      使用datar很容易做到这一点:

      >>> from datar.all import f, nest
      >>> from datar.datasets import iris
      >>> iris_nested = iris >> nest(data=~f.Species)
      >>> iris_nested
            Species       data
           <object>   <object>
      0      setosa  <DF 50x4>
      1  versicolor  <DF 50x4>
      2   virginica  <DF 50x4>
      >>> iris_nested.iloc[0, 1]
          Sepal_Length  Sepal_Width  Petal_Length  Petal_Width
             <float64>    <float64>     <float64>    <float64>
      0            5.1          3.5           1.4          0.2
      1            4.9          3.0           1.4          0.2
      2            4.7          3.2           1.3          0.2
      3            4.6          3.1           1.5          0.2
      4            5.0          3.6           1.4          0.2
      5            5.4          3.9           1.7          0.4
      6            4.6          3.4           1.4          0.3
      7            5.0          3.4           1.5          0.2
      8            4.4          2.9           1.4          0.2
      9            4.9          3.1           1.5          0.1
      10           5.4          3.7           1.5          0.2
      11           4.8          3.4           1.6          0.2
      12           4.8          3.0           1.4          0.1
      13           4.3          3.0           1.1          0.1
      14           5.8          4.0           1.2          0.2
      15           5.7          4.4           1.5          0.4
      16           5.4          3.9           1.3          0.4
      17           5.1          3.5           1.4          0.3
      18           5.7          3.8           1.7          0.3
      19           5.1          3.8           1.5          0.3
      20           5.4          3.4           1.7          0.2
      21           5.1          3.7           1.5          0.4
      22           4.6          3.6           1.0          0.2
      23           5.1          3.3           1.7          0.5
      24           4.8          3.4           1.9          0.2
      25           5.0          3.0           1.6          0.2
      26           5.0          3.4           1.6          0.4
      27           5.2          3.5           1.5          0.2
      28           5.2          3.4           1.4          0.2
      29           4.7          3.2           1.6          0.2
      30           4.8          3.1           1.6          0.2
      31           5.4          3.4           1.5          0.4
      32           5.2          4.1           1.5          0.1
      33           5.5          4.2           1.4          0.2
      34           4.9          3.1           1.5          0.2
      35           5.0          3.2           1.2          0.2
      36           5.5          3.5           1.3          0.2
      37           4.9          3.6           1.4          0.1
      38           4.4          3.0           1.3          0.2
      39           5.1          3.4           1.5          0.2
      40           5.0          3.5           1.3          0.3
      41           4.5          2.3           1.3          0.3
      42           4.4          3.2           1.3          0.2
      43           5.0          3.5           1.6          0.6
      44           5.1          3.8           1.9          0.4
      45           4.8          3.0           1.4          0.3
      46           5.1          3.8           1.6          0.2
      47           4.6          3.2           1.4          0.2
      48           5.3          3.7           1.5          0.2
      49           5.0          3.3           1.4          0.2
      

      它与dplyr/tidyr API 保持一致。

      我是包的作者。如果您有任何问题,请随时提交问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-09
        • 2012-06-18
        • 2023-03-31
        • 1970-01-01
        • 2014-04-02
        • 2019-10-31
        • 1970-01-01
        相关资源
        最近更新 更多