【问题标题】:delimiting ndarray into different columns将ndarray划分为不同的列
【发布时间】:2015-10-05 08:40:52
【问题描述】:

我有一个数据集,其中第一列有一个系列,第二列有一个 ndarray。 ndarray 由“,”分隔值组成。 如何将值拆分为不同的列?

data sample:


             id                               values
    0      390725715                 (service-selection-page, 1, 3)
    1      682669054                 (mobile-apps full-page, 1, 12)
    2      770810604                (service-selection-page, 2, 41)
    3     1009039867                (list-property full-page, 1, 7)
    4     1523526830                 (service-selection-page, 2, 1)
    5     1495892895                 (mobile-apps full-page, 1, 24)
    6      975125144                (service-selection-page, 1, 37)

这里,id 是一个系列,values 是 ndarray。

Expected output:
          id                     values             0     1
0      390725715         service-selection-page     1     3
1      682669054         mobile-apps full-page      1     12
2      770810604         service-selection-page     2     41
3     1009039867         list-property full-page    1     7
4     1523526830         service-selection-page     2     1
5     1495892895         mobile-apps full-page      1     24
6      975125144         service-selection-page     1     37

提前致谢!

【问题讨论】:

    标签: python pandas multidimensional-array csv


    【解决方案1】:

    df['values'].apply(lambda x: pd.Series(x)) 是您想要做的。

    例如,如果您的df 是这样的

    In [38]: df = pd.DataFrame([[390 , pd.np.array(('service-selection-page', 1, 3))],
                                [110 , pd.np.array(('page', 1, 3))]],
                               columns=['id', 'values'])
    In [39]: df
    Out[39]:
        id                          values
    0  390  [service-selection-page, 1, 3]
    1  110                    [page, 1, 3]
    

    其中,values 包含 numpy 数组,applylambda x: pd.Series(x) on df['values']

    In [40]: df['values'].apply(lambda x: pd.Series(x))
    Out[40]:
                            0  1  2
    0  service-selection-page  1  3
    1                    page  1  3
    

    而且,您可以使用连接来扩展列。

    In [41]: df.join(df['values'].apply(lambda x: pd.Series(x)))
    Out[41]:
        id                          values                       0  1  2
    0  390  [service-selection-page, 1, 3]  service-selection-page  1  3
    1  110                    [page, 1, 3]                    page  1  3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2023-03-21
      • 2017-12-28
      • 1970-01-01
      • 2019-05-09
      相关资源
      最近更新 更多