Pandas Dataframe:将多个列拆分为两列

时间:2017-05-01 08:07:05

标签: python pandas

我有一个名为df的pandas数据框,如下所示:

0   2J-AAB1 AA  AA  CC  CC  AA  AA  CC  AA  CC
1   2J-AAB4 AA  TA  TC  TC  GA  AA  CC  AA  CC
2   2J-AAB6 AA  TA  CC  CC  AA  AA  CC  AA  CC
3   2J-AAB8 AA  TT  TT  TT  GG  AA  TC  CC  CC
4   2J-AAB9 AA  TT  TT  TT  GG  AA  TC  CC  CC
5   2J-AABA AA  AA  CC  CC  GA  AG  CC  AA  CG
6   2J-AABE AA  TT  TT  TT  GG  AA  TC  CA  CC
7   2J-AABF AA  AA  CC  CC  AA  AA  CC  AA  CC
8   2J-AABH AA  TT  TT  TT  GG  AA  CC  AA  CC
9   2J-AABI AA  AA  CC  CC  AA  AA  CC  AA  CG

我想分割像" AA,AT,CC"等全部分为两列并获得新的数据框架,如:

0   2J-AAB1 A   A   A   A   C   C   C   C   A   A   A   A   C   C   A   A   C   C
1   2J-AAB4 A   A   T   A   T   C   T   C   G   A   A   A   C   C   A   A   C   C
2   2J-AAB6 A   A   T   A   C   C   C   C   A   A   A   A   C   C   A   A   C   C
3   2J-AAB8 A   A   T   T   T   T   T   T   G   G   A   A   T   C   C   C   C   C
4   2J-AAB9 A   A   T   T   T   T   T   T   G   G   A   A   T   C   C   C   C   C
5   2J-AABA A   A   A   A   C   C   C   C   G   A   A   G   C   C   A   A   C   G
6   2J-AABE A   A   T   T   T   T   T   T   G   G   A   A   T   C   C   A   C   C
7   2J-AABF A   A   A   A   C   C   C   C   A   A   A   A   C   C   A   A   C   C
8   2J-AABH A   A   T   T   T   T   T   T   G   G   A   A   C   C   A   A   C   C
9   2J-AABI A   A   A   A   C   C   C   C   A   A   A   A   C   C   A   A   C   G

是否有一种pythonic方式来制作它?任何建议都表示赞赏..提前致谢

3 个答案:

答案 0 :(得分:4)

试试这个:

In [60]: x = df.set_index(1).stack().str.extractall('(.)').unstack([-2, -1]).reset_index()

In [61]: x.columns = np.arange(len(x.columns))

In [62]: x
Out[62]:
        0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18
0  2J-AAB1  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
1  2J-AAB4  A  A  T  A  T  C  T  C  G  A  A  A  C  C  A  A  C  C
2  2J-AAB6  A  A  T  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
3  2J-AAB8  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  C  C  C
4  2J-AAB9  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  C  C  C
5  2J-AABA  A  A  A  A  C  C  C  C  G  A  A  G  C  C  A  A  C  G
6  2J-AABE  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  A  C  C
7  2J-AABF  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
8  2J-AABH  A  A  T  T  T  T  T  T  G  G  A  A  C  C  A  A  C  C
9  2J-AABI  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  G

答案 1 :(得分:1)

你有一个很好的答案,但我开始输入这个,所以我想把它留下来。

您可以将applysplitlist一起使用,以输出到多个列。 对于带有标签的数据框:

            A    B
0   "2J-AAB1" "AA" 
1   "2J-AAB4" "AA"  
2   "2J-AAB6" "AA" 
3   "2J-AAB8" "AA"


df['B1'], df['B2'] = zip(*df['B'].apply(lambda x: list(x)))

这会给你:

         A   B B2 B1
0  2J-AAB1  AA  A  A
1  2J-AAB4  AA  A  A
2  2J-AAB6  AA  A  A
3  2J-AAB8  AA  A  A

对于更多列或具有特定列名称,可以执行以下操作:

for i in df.columns[1:]:
    df['{}1'.format(i)], df['{}2'.format(i)] = zip(*df[i].apply(lambda x: list(x)))

这给出了:

         0   1   2   3   4   5   6   7   8   9 11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 91 92
0  2J-AAB1  AA  AA  CC  CC  AA  AA  CC  AA  CC  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
1  2J-AAB4  AA  TA  TC  TC  GA  AA  CC  AA  CC  A  A  T  A  T  C  T  C  G  A  A  A  C  C  A  A  C  C
2  2J-AAB6  AA  TA  CC  CC  AA  AA  CC  AA  CC  A  A  T  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
3  2J-AAB8  AA  TT  TT  TT  GG  AA  TC  CC  CC  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  C  C  C
4  2J-AAB9  AA  TT  TT  TT  GG  AA  TC  CC  CC  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  C  C  C
5  2J-AABA  AA  AA  CC  CC  GA  AG  CC  AA  CG  A  A  A  A  C  C  C  C  G  A  A  G  C  C  A  A  C  G
6  2J-AABE  AA  TT  TT  TT  GG  AA  TC  CA  CC  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  A  C  C
7  2J-AABF  AA  AA  CC  CC  AA  AA  CC  AA  CC  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
8  2J-AABH  AA  TT  TT  TT  GG  AA  CC  AA  CC  A  A  T  T  T  T  T  T  G  G  A  A  C  C  A  A  C  C
9  2J-AABI  AA  AA  CC  CC  AA  AA  CC  AA  CG  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  G

答案 2 :(得分:0)

非常有趣的问题。可以按如下方式逐步解决:

dfpart = df.iloc[:,1:]                    # get columns to be split
ll = dfpart.values                        # get values as list of lists
sl = list(map(lambda x: "".join(x), ll))  # join all rows into strings
sl = list(map(list, sl))                  # split strings to lists of characters
newdf = pd.DataFrame(data=sl)             # create dataframe from new lists
newdf = pd.concat([df.iloc[:,0], newdf], axis=1) # restore first column
newdf.columns= range(len(newdf.columns))  # correct column numbers; 
print(newdf)

输出:

        0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18
0  2J-AAB1  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
1  2J-AAB4  A  A  T  A  T  C  T  C  G  A  A  A  C  C  A  A  C  C
2  2J-AAB6  A  A  T  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
3  2J-AAB8  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  C  C  C
4  2J-AAB9  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  C  C  C
5  2J-AABA  A  A  A  A  C  C  C  C  G  A  A  G  C  C  A  A  C  G
6  2J-AABE  A  A  T  T  T  T  T  T  G  G  A  A  T  C  C  A  C  C
7  2J-AABF  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  C
8  2J-AABH  A  A  T  T  T  T  T  T  G  G  A  A  C  C  A  A  C  C
9  2J-AABI  A  A  A  A  C  C  C  C  A  A  A  A  C  C  A  A  C  G