Pandas:将列拆分成多行

Mat*_*und 7 python pandas

我有一个关于将数据帧列中的列表拆分为多行的问题.

假设我有这个数据帧:

  Job position   Job type  id
0          [6]        [1]   3
1       [2, 6]  [3, 6, 5]   4
2          [1]        [9]  43
Run Code Online (Sandbox Code Playgroud)

我想要每一个数字组合,所以最终的结果是:

   id    Job position  Job type
0   3         6.0       1.0
1   4         2.0       3.0
2   4         2.0       6.0
3   4         2.0       5.0
4   4         6.0       3.0
5   4         6.0       6.0
6   4         6.0       5.0
7  43         1.0       9.0
Run Code Online (Sandbox Code Playgroud)

因为现在我得到这个结果:

   id    Job position  Job type
0   3         6.0       1.0
1   4         2.0       3.0
2   4         6.0       6.0
3   4         NaN       5.0
4  43         1.0       9.0
Run Code Online (Sandbox Code Playgroud)

为了得到上面的结果,我做了:

df = df.set_index(['id'])
(df.apply(lambda x: pd.DataFrame(x.tolist(),index=x.index)
                        .stack()
                        .rename(x.name)).reset_index())
Run Code Online (Sandbox Code Playgroud)

Ami*_*ory 9

与斯科特波士顿的建议类似,我建议你单独爆炸列,然后将它们合并在一起.

例如,对于"工作职位":

>>> df['Job position'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
    value
index   
0   6.0
1   2.0
2   1.0
1   6.0
Run Code Online (Sandbox Code Playgroud)

所有在一起:

df = pd.DataFrame({'Job position': [[6], [2, 6], [1]], 'Job type': [[1], [3, 6, 5], [9]], 'id': [3, 4, 43]})
jobs = df['Job position'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
types = df['Job type'].apply(pd.Series).reset_index().melt(id_vars='index').dropna()[['index', 'value']].set_index('index')
>>> pd.merge(
    pd.merge(
        jobs,
        types,
        left_index=True,
        right_index=True),
    df[['id']],
    left_index=True,
    right_index=True).rename(columns={'value_x': 'Job positions', 'value_y': 'Job type'})
Job positions   Job type    id
0   6.0 1.0 3
1   2.0 3.0 4
1   2.0 6.0 4
1   2.0 5.0 4
1   6.0 3.0 4
1   6.0 6.0 4
1   6.0 5.0 4
2   1.0 9.0 43
Run Code Online (Sandbox Code Playgroud)


piR*_*red 5

使用理解

pd.DataFrame([
    [p, t, i] for P, T, i in df.values
    for p in P for t in T
], columns=df.columns)

   Job position  Job type  id
0             6         1   3
1             2         3   4
2             2         6   4
3             2         5   4
4             6         3   4
5             6         6   4
6             6         5   4
7             1         9  43
Run Code Online (Sandbox Code Playgroud)

迭代的替代方案 values

pd.DataFrame([
    [p, t, i] for P, T, i in df.itertuples(index=False)
    for p in P for t in T
], columns=df.columns)
Run Code Online (Sandbox Code Playgroud)
z = zip(df['Job position'], df['Job type'], df['id'])
pd.DataFrame([
    [p, t, i] for P, T, i in z
    for p in P for t in T
], columns=df.columns)
Run Code Online (Sandbox Code Playgroud)

概括此解决方案以适应任意数量的列

pd.DataFrame([
    [p, t] + a for P, T, *a in df.values
    for p in P for t in T
], columns=df.columns)

   Job position  Job type  id
0             6         1   3
1             2         3   4
2             2         6   4
3             2         5   4
4             6         3   4
5             6         6   4
6             6         5   4
7             1         9  43
Run Code Online (Sandbox Code Playgroud)