将ndarray划分为不同的列

时间:2015-10-05 08:40:52

标签: python pandas multidimensional-array delimited-text

我有一个数据集,其中第一列中有一个系列,第二列中有一个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是一个系列,值是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

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

其中,apply包含lambda x: pd.Series(x)上的numpy数组,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

并且,您可以使用join来扩展列。

modelBuilder.Entity<Person>().HasRequired(p=>p.Creation).WithOptional(a=>a.Person)
相关问题